Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

javascript - 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.

question from:https://stackoverflow.com/questions/17301269/can-websocket-addresses-carry-parameters

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...