最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get websocket url path from php or node.js - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question asked Nov 18, 2013 at 23:57 Lau RaymondLau Raymond 1331 gold badge2 silver badges5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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)
})

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

发布评论

评论列表(0)

  1. 暂无评论