Can WebSocket addresses carry parameters?

后端 未结 2 637
忘掉有多难
忘掉有多难 2020-12-24 07:27

Is ws://myserver.com/path?param=1 a valid WebSocket address ?

The address http://myserver.com/path?param=1 (notice it\'s now http

相关标签:
2条回答
  • 2020-12-24 07:41

    ws://myserver.com/path?param=1 is a valid WebSocket URI. However, the way that your WebSocket server application can access the path and query string will differ depending on what WebSocket server framework you are using.

    If you are using the Node.js einaros/ws library, then in your websocket connection object will have the full path with the query string at upgradeReq.url.

    For example this:

    wss.on('connection', function(ws) {
        console.log("url: ", ws.upgradeReq.url);
    };
    

    will print url: /path?param=1 when you connect to ws://myserver.com/path?param=1.

    0 讨论(0)
  • 2020-12-24 07:48

    To use with latest ws, the connection callback now has another argument - which is req.

    wss.on("connection", (ws, req) => {
       console.log(`Conn Url ${req.url}`);
    });
    
    0 讨论(0)
提交回复
热议问题