0

I am trying to create a websocket connection to 'ws://echo.websocket.org' with socket.io-client, here is my code:

const url = 'ws://echo.websocket.org'
const io = require('socket.io-client');
console.log('websocket conneting : ', url);

const socket = new io(url, {
    transports: ['websocket'],
    path: '/',
});

socket.on('connect', function(){
    console.log('connect')
});

the result is that the 'connect' event never happened, and no event happened, I have catched the headers by wireshark:

GET /?EIO=3&transport=websocket HTTP/1.1
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: XHrqdeZVhzRlp1eijFgMfw==
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Host: echo.websocket.org

/*
response
 */
HTTP/1.1 101 Web Socket Protocol Handshake
Connection: Upgrade
Date: Thu, 27 Jul 2017 09:22:50 GMT
Sec-WebSocket-Accept: GBUm4nbYv7kGp9H24yfqX9hh930=
Server: Kaazing Gateway
Upgrade: websocket

it seems that the connection is established successfully, so, why the 'connect' event doesn`t happened?

itenyh
  • 1,921
  • 2
  • 23
  • 38
  • `socket.io` isn't a Websocket client and requires a `socket.io` server to communicate with. – robertklep Jul 27 '17 at 10:04
  • so, the server must be created with socket.io? but the header shows websocket connection is established. – itenyh Jul 27 '17 at 10:09
  • One of the transports that `socket.io` supports is Websocket, but it requires a server that "talks" the protocol that it used on top of that transport. Since that's not the case in your example, there's no `connect` event because the client never connects to a `socket.io` server. – robertklep Jul 27 '17 at 10:13
  • I understand, thanks! @robertklep – itenyh Jul 27 '17 at 10:33
  • More info here: https://stackoverflow.com/questions/22232023/can-i-use-socket-io-client-to-connect-to-a-standard-websocket – robertklep Jul 27 '17 at 10:40

0 Answers0