服务端使用socket.io编写,客户端也必须使用socket.io吗,可以直接使用浏览器提供的websocket对象吗?
发布于 11 年前 作者 sleefd 17302 次浏览 最后一次编辑是 8 年前

求解答,非常紧急。

14 回复

看了这两篇东西,但还是不明白接口一样指什么?

@sleefd 可以,前提是你必须懂socket.io的交互协议,话说如果都明确了是websocket,可以考虑自己实现了,socket.io的协议可以参考下

@dengqiao Hi,我的理解是服务器端处理好handshake,建立好连接通道就可以了。然后客户端不需要什么协议上的处理,直接使用浏览器websocket对象就好了。 socket.io是客户端必须实现它自己定义的交互协议,才能完成和服务器端socket.io的交互嘛?

@sleefd 对的。它正是对 WebSocket 进行了封装所以使用起来非常方便,而这层封装,从技术上说就是对它协议的实现。

@alsotang 啊啊啊 好吧 那有对客户端协议没有要求的node.js websocket module嘛?

@sleefd 客户端跟 Node.js 没有关系,而且为何你不能直接直接使用 Socket.io 的那个前端库呢?你可以在帖子里面把你的需求再说详细点吗?

@alsotang 不是的。我的意思是我要以node.js作为服务器,与浏览器提供的websockt对象直接通信,不考虑对客户端的websocket做进一层的封装。 我使用的一个框架提供了与浏览器类似的websocket对象,既可以使用在html5中,也可以直接以应用的方式存在,为了保持两边的兼容性,不考虑在这个框架中加入新的东西。

@sleefd 那你可以考虑去找一个直接抡 websocket 的 Node.js module 吧。我目前也没有推荐的,因为没这样用过。

浏览器 发送 GET /socket.io/?EIO=3&transport=polling&t=LZugHyU&b64=1 HTTP/1.1 然后服务器 返回 97:0{“sid”:“iQnJvIk_g0ra2-PuAAAD”,“upgrades”:[“websocket”],“pingInterval”:25000,“pingTimeout”:60000}

这些字段值是根据什么生成的?

不能,必须是服务端和客户端都是使用socket.io实现的才能。证据:https://socket.io/docs/#What-Socket-IO-is 有一句话: What Socket.IO is not Socket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the ack id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server either. Please see the protocol specification here.

回到顶部