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

Lau*_*ond 3 javascript php node.js

我的html代码

var url='ws://localhost:8000/abc';
  socket=new WebSocket(url);
  socket.onopen=function(){
    log('Success');
  }
Run Code Online (Sandbox Code Playgroud)

在 php 或 node.js 中如何获取 url 路径abc

Vid*_*dar 6

如果您为节点使用ws 库,您可以从ws.connection请求对象的第二个参数中获取 URL :

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

wss.on('connection', (ws, req) => {
  console.log(req.url)
})
Run Code Online (Sandbox Code Playgroud)


Som*_*ens 0

PHP 或 Node.js 中没有使用 websocket 的原生方法。您需要使用某种类型的库。幸运的是,有很多可供选择:

Node.JS

PHP