How to connect with socket.io from a ws client?

前端 未结 2 2009
刺人心
刺人心 2020-12-28 21:10

I have a very simple socket.io chat example, and the server side code is like this:

https://github.com/js-demos/socketio-chat-demo/blob/master/index.js



        
2条回答
  •  抹茶落季
    2020-12-28 22:05

    Agreeing with @freewind 's answer, I would like to go little deeper with the detail description.

    ws://localhost:3000/socket.io/?EIO=3&transport=websocket
    

    The specification can be described as

    +----------------+--------------------+
    |      Elm       |        Desc        |
    +----------------+--------------------+
    | WS             | scheme             |
    | localhost:3000 | host               |
    | socket.io      | path               |
    | EIO=3          | EngineIO version 3 |
    | transport      | websocket          |
    +----------------+--------------------+
    

    If the transport is set to websocket then It can be testable with any WebSocket client as it's upgrading the connection with the WebSocket protocol.

    We can easily test and debug it with the Firecamp WebSocket Client. The connection will look like this below image.

    Firecamp is the GUI client for the SocketIO and WebSocket.

    1. SocketIO

    The configuration of the following screen will be like

    import io from "socket.io-client";
    const socket = io( "http://localhost:3000/socket.io", 
     {
      "path": "/socketio",
      "transports": [
        "websocket"
      ]
     }
    );
    

    2. WebSocket

    The same socket endpoint, We can test it with the WebSocket client as showing in following images

    - WS connection

    - WS feed data

提交回复
热议问题