Web插座 - 我可以在使用socket.connect()方法连接到服务器时发送有关客户端的额外信息

发布于 2025-02-06 10:24:27 字数 650 浏览 3 评论 0原文

我正在从事WEBRTC项目,并且具有输入视频流的客户端,将显示输入流和服务器的客户端。我正在使用WEBRTC和WebSockets。每个客户端都连接到信号服务器。我想知道服务器如何知道连接的客户端是否是输入视频流的客户端,或者是将显示一些流的客户端。在将客户端连接到服务器的那一刻,我可以发送一些额外的信息吗?客户端用python的JavaScript和服务器编写。也许我可以在socket.connect()方法中使用一些参数?

服务器处理连接:

@sio.event
async def connect(sid, environ):
    print('Connected', sid)
    sessionIDs.append(sid)

    await sio.emit('ready', room=ROOM, skip_sid=sid)
    sio.enter_room(sid, ROOM)

将客户端连接到服务器:

socket.connect();

I am working on webrtc project and I have client(s) with input video stream, client that will show input stream and server. I am using webrtc and websockets. Every client is connected to signaling server. I want to know how server can know if connected client is client with input video stream or it is client that will show some streams. Can I send some extra info in the moment of connecting client side to server? Client is written in Javascript and server in Python. Maybe I can use some parameters in socket.connect() method?

Server handling connecting:

@sio.event
async def connect(sid, environ):
    print('Connected', sid)
    sessionIDs.append(sid)

    await sio.emit('ready', room=ROOM, skip_sid=sid)
    sio.enter_room(sid, ROOM)

Connecting client to server:

socket.connect();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上得阳光 2025-02-13 10:24:28

只需在WebSocket URL中添加数据作为获取参数,或使用其他路径。

const socket = io("ws://localhost:3000?arg1=1&arg2=2");
// or
const socket = io("ws://localhost:3000/path1");

server 以获取它们:

io.on("connection", (socket) => {
  console.log(socket.handshake.query); // prints { x: "42", EIO: "4", transport: "polling" }
});

Just add your data as GET parameter in the websocket url, or use a differente path.

const socket = io("ws://localhost:3000?arg1=1&arg2=2");
// or
const socket = io("ws://localhost:3000/path1");

And on the server to get them:

io.on("connection", (socket) => {
  console.log(socket.handshake.query); // prints { x: "42", EIO: "4", transport: "polling" }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文