8

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?

2 Answers 2

17

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)
})
Sign up to request clarification or add additional context in comments.

Comments

0

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:

Node.JS

PHP

2 Comments

In node.js, wsServer.on('request', function(request) { console.log((new Date()) + ' Connection from origin ' + request.origin + '.');, but no request.url
What are you trying to say?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.