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 |3 Answers
Reset to default 12In 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
版权声明:本文标题:javascript - websocket send parameter in connection? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738720963a2108742.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ws
andwebsocket
... (they are not in core) which one are you using? Therequire
statement will tell you. – ouni Commented Jun 15, 2018 at 22:22