Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can WebSocket addresses carry parameters?

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

The address http://myserver.com/path?param=1 (notice it's now http and not ws) works fine with wscat, but I can't get it working on the browser.

like image 665
João Pinto Jerónimo Avatar asked Jun 25 '13 15:06

João Pinto Jerónimo


People also ask

Can WebSockets send binary data?

WebSockets support sending binary messages, too. To send binary data, one can use either Blob or ArrayBuffer object. Instead of calling the send method with string, you can simply pass an ArrayBuffer or a Blob .

Can WebSockets send JSON?

To create a WebSocket and open the connection to FME Server use getWebSocketConnection method. The result is an open WebSocket you can use in your application to communicate with FME Server. This example sends a basic JSON object as a string to the server, and displays the response from the server.

What is payload in WebSocket?

Data is transferred through a WebSocket as messages, each of which consists of one or more frames containing the data you are sending (the payload). In order to ensure the message can be properly reconstructed when it reaches the client each frame is prefixed with 4–12 bytes of data about the payload.

How do I get data from a WebSocket?

First, you need to copy your web browser's header to here and use json. dumps to convert it into the string format. After that, create the connection to the server by using create_connection . Then, perform the handshake by sending the message, and you will be able to see the data on your side.


2 Answers

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.

like image 151
kanaka Avatar answered Sep 27 '22 20:09

kanaka


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}`); }); 
like image 45
singhspk Avatar answered Sep 27 '22 21:09

singhspk