Socket.io 客户端无法使用 websocket 服务器
Socket.io client not working with websocket server
我使用 https://github.com/gorilla/websocket 在 go 中编写了一个 websocket 服务器。
在客户端上,前端团队想要使用 Socket.io,但无法正常工作。
使用普通的 websockets 一切正常。我对Socket.io了解不多,只是在网上看的,没有找到任何果断和最新的答案。
有没有可能让 Socket.io 与普通的 websocket 服务器一起工作?
Socket.IO 客户端错误:超时。
Socket.IO 客户代码:
var token = "valid_token";
var socket = io(
'ws://localhost:6060',
{
transports: ['websocket'],
path: '/ws/dispatchers',
query: {
token: token
},
}
);
socket.on('connect_error', (error) => {
console.log("------connect_error------");
console.log(error);
});
socket.on('connect_timeout', (timeout) => {
console.log("------timeout-----");
console.log(timeout);
});
socket.on('connect', function(){
console.log("---------connect---------");
});
socket.on('event', function(data){
console.log(data)
});
socket.on('disconnect', function(){
console.log("---------disconnect---------")
});
socket.on('error', function(err){
console.log("--------error------------");
console.log(err)
});
socket.on('ping', () => {
console.log("--------ping------------");
});
socket.on('pong', (latency) => {
console.log("--------pong------------");
});
后端错误:websocket:关闭 1005(无状态)
后端代码,与以下相同:
https://github.com/gorilla/websocket/tree/master/examples/chat
来自 Socket.io 文档
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 packet 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.
我使用 https://github.com/gorilla/websocket 在 go 中编写了一个 websocket 服务器。 在客户端上,前端团队想要使用 Socket.io,但无法正常工作。 使用普通的 websockets 一切正常。我对Socket.io了解不多,只是在网上看的,没有找到任何果断和最新的答案。
有没有可能让 Socket.io 与普通的 websocket 服务器一起工作?
Socket.IO 客户端错误:超时。
Socket.IO 客户代码:
var token = "valid_token";
var socket = io(
'ws://localhost:6060',
{
transports: ['websocket'],
path: '/ws/dispatchers',
query: {
token: token
},
}
);
socket.on('connect_error', (error) => {
console.log("------connect_error------");
console.log(error);
});
socket.on('connect_timeout', (timeout) => {
console.log("------timeout-----");
console.log(timeout);
});
socket.on('connect', function(){
console.log("---------connect---------");
});
socket.on('event', function(data){
console.log(data)
});
socket.on('disconnect', function(){
console.log("---------disconnect---------")
});
socket.on('error', function(err){
console.log("--------error------------");
console.log(err)
});
socket.on('ping', () => {
console.log("--------ping------------");
});
socket.on('pong', (latency) => {
console.log("--------pong------------");
});
后端错误:websocket:关闭 1005(无状态)
后端代码,与以下相同:
https://github.com/gorilla/websocket/tree/master/examples/chat
来自 Socket.io 文档
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 packet 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.