Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get websocket url path from php or node.js

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?

like image 414
Lau Raymond Avatar asked Dec 26 '22 17:12

Lau Raymond


1 Answers

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)
})
like image 61
Vidar Avatar answered Dec 28 '22 09:12

Vidar