简体   繁体   English

如何从php或node.js获取websocket URL路径

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

My html code 我的html代码

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 ? 在php或node.js中如何获取url路径abc

There isn't a native way to work with websockets in PHP or Node. 没有原生方法可以在PHP或Node中使用websocket。 You'll need to use a library of some sort. 您需要使用某种类型的库。 Fortunately, there are plenty to choose from: 幸运的是,有很多可供选择:

Node.JS 节点JS

PHP PHP

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: 如果您将ws库用于节点 ,则可以从第二个参数获取ws.connection的URL,该参数是请求对象:

const server = new http.createServer()
const wss = new WebSocket.Server({ server })

wss.on('connection', (ws, req) => {
  console.log(req.url)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM