admin管理员组

文章数量:1208155

I need to create a very simple text chat using nodejs and Websocket.

I have everything set up and working.

I now need to allow users to send a parameter in the websocket connection URL.

Like so: websocket = new WebSocket("ws://test-website:8080/?id=55");

in my reserach I came across quite a few similar questions and found out that I can pass the parameters in the URL like the example above.

However, none of them mentions or explains how to get this parameter (55 in this example) in server.js

wss.on('connection', function(ws) {

///I need to get the parameter here////

});

Any advice on this issue?

I need to create a very simple text chat using nodejs and Websocket.

I have everything set up and working.

I now need to allow users to send a parameter in the websocket connection URL.

Like so: websocket = new WebSocket("ws://test-website.net:8080/?id=55");

in my reserach I came across quite a few similar questions and found out that I can pass the parameters in the URL like the example above.

However, none of them mentions or explains how to get this parameter (55 in this example) in server.js

wss.on('connection', function(ws) {

///I need to get the parameter here////

});

Any advice on this issue?

Share Improve this question edited Jun 5, 2023 at 20:51 desertnaut 60.3k31 gold badges151 silver badges177 bronze badges asked Jun 15, 2018 at 22:12 william wollywilliam wolly 3451 gold badge5 silver badges16 bronze badges 4
  • Which websocket server-side library are you using? – ouni Commented Jun 15, 2018 at 22:18
  • @ouni, I have mentioned that in my question. I am using node js. – william wolly Commented Jun 15, 2018 at 22:19
  • Node has multiple websocket implementations, such as ws and websocket... (they are not in core) which one are you using? The require statement will tell you. – ouni Commented Jun 15, 2018 at 22:22
  • @ouni, I am using WebSocketServer = require('ws').Server, – william wolly Commented Jun 15, 2018 at 22:25
Add a comment  | 

3 Answers 3

Reset to default 12

In your websocket request handler, it is normally passed the web socket as the first argument; the incoming HTTP request is passed as a second argument:

webSocketServer.on('connection', function connection(ws_client_stream, incoming_request) {
  console.log(incoming_request.url);
});

From there, you can use url.parse to get the components of the URL, such as the query string that you are interested in.

For websocket library in nodejs (websocket.js npm) :

wsServer.on('request', function(request){
    var FullURL = request.resourceURL.query;
    console.log(FullURL);
    // . . your code  . . //
}

link = wss://localhost/?param=value
output

{ param : 'value' }

here request.resourceURL retuen a object

Url {
  protocol: null,
  slashes: null,
  auth: null,
  host: null,
  port: null,
  hostname: null,
  hash: null,
  search: '?id=65&encoding=text',
  query: { id: '65', encoding: 'text' },
  pathname: '/',
  path: '/?id=65&encoding=text',
  href: '/?id=65&encoding=text' }

If you're using 'websocket' lib, the following code retrieves your queryStrings:

wsServer.on('request', function(request) 
{
    console.log(request.resourceURL.query);
}

Note 1: You don't need to parse anything here.

Note 2: Though it has been answered years ago, this answer goes to those in case where people find the above-mentioned answer more complicated.

本文标签: javascriptwebsocket send parameter in connectionStack Overflow