My html code
var url='ws://localhost:8000/abc';
socket=new WebSocket(url);
socket.onopen=function(){
log('Success');
}
in php or node.js how to get url path abc?
There isn't a native way to work with websockets in PHP or Node. You'll need to use a library of some sort. Fortunately, there are plenty to choose from:
If you're using the ws library for node you can get the URL from the second argument to ws.connection
which is the request object:
const server = new http.createServer()
const wss = new WebSocket.Server({ server })
wss.on('connection', (ws, req) => {
console.log(req.url)
})
const server = new http.createServer()
const wss = new WebSocket.Server({ server })
wss.on('connection', (ws, req) => {
console.log(req.url)
})
太帅了,居然在回调函数的第二个参数能够获取到websocket的路径,谢谢大神的指点
或者可以通过console.log('ws.url:',ws.upgradeReq.url)获取到访问路径