Connection closed pending confirmation response

I am writing a node program and I am facing a lot of difficulty.

server side code below:

var express=require("express");
var app=express();
var socketio=require("socket.io");
var server=require("http").Server(app);
var ws=socketio.listen(server);
app.use(express.static('public'));
app.listen(3000);
ws.on('connection',function(socket){
socket.on("message",function(msg){
    console.log("got:"+msg);
    socket.send('pong');
    });
});

      

client side code is below:

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>websocket echo</title>
</head>
<body>
<h1>websocket echo</h1>
<h2>latency:<span id="latency"></span>ms</h2>
<script>

var lastMessage;
window.onload=function(){
    //create socket

    var ws=new WebSocket("ws://127.0.0.1:3000");
    ws.onopen=function(){
        //send first ping
        ping();
    };
    // 监听Socketηš„ε…³ι—­
    ws.onclose = function(event) {
        console.log('Client notified socket has closed',event);
    };
    ws.onmessage=function(ev){
        console.log("got:"+ev.data);

        document.getElementById("latency").innerHTML=new Date-lastMessage;
        ping();
    };
    function ping(){
        lastMessage= + new Date;
        ws.send("ping");
    }
}
</script>
</body>
</html>

      

there is a tip in chrome console: WebSocket connection with 'ws: //127.0.0.1: 3000 /' failed: connection closed before receiving response (index) handshake: 16 Client socket closed CloseEvent

+3


source to share


1 answer


As mentioned in the comments, this is because socket.io needs to be linked to it by its own client. You must either use websockets or socket.io on both sides.



0


source







All Articles