我可以使用 socket.io-client 连接到标准的 websocket 吗? [英] can I use socket.io-client to connect to a standard websocket?

查看:38
本文介绍了我可以使用 socket.io-client 连接到标准的 websocket 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用socket.io-client连接Go编写的websocket服务器.我已使用节点 WebSocket 库 (npm) 成功连接.所以工作的 Websocket 代码看起来像:

Trying to use socket.io-client to connect to a websocket server that is written in Go. I've successfully connected using the node WebSocket library (npm). So the working Websocket code looks like:

  goSocketPort = 6060
  url = "ws://localhost:#{goSocketPort}/streamresults/"
  ws = new WebSocket(url)

  ws.on('open', ->
    log "socket opened"
  )

  ws.on('message', (message) ->
      console.log('received: %s', message)
    #log "Socket message: #{JSON.stringify message}"
  )

很简单,而且很有效——另一端的套接字以设定的频率发送消息.但我最初尝试使用 socket.io-client (npm) 并且只是不能让它去.它当然将 websocket 列为它的首选传输方式,但我能不能让它连接该死的:

Pretty easy and it works -- the socket on the other end sends messages on a set frequency. But I initially tried with socket.io-client (npm) and just couldn't get it to go. It certainly lists websocket as its first-preference transport, but damn if I can get it to connect:

socket = ioClient.connect("#{url}", {port: goSocketPort, transports: ['xhr-polling', 'websocket']}) 

socket.on("connect", (r) ->
  log "connected to #{url}"
)

连接永远不会发生,所以没有任何 on 事件被触发,代码立即退出.我试过:将端口从 url 中删除并将其添加到选项中,离开传输选项(根据文档,这意味着全部")并使用 http url.socket-io.client 是否无法连接到标准"websocket?

The connection never happens, so none of the on events are fired and the code exits right away. I've tried: leaving the port off the url and adding it in the options, leaving off the transports option (which means "all" according to the docs) and using an http url. Is socket-io.client not capable of connecting to a "standard" websocket?

推荐答案

根据我们的聊天记录,您似乎被这句话误导了:

Based on our chat, it looks like you were misled by this quote:

socket.io 客户端基本上是一个简单的 HTTP Socket 接口实现.当用户的浏览器不支持 WebSocket 时,它看起来与 WebSocket 类似,同时提供附加功能并利用其他传输.

The socket.io client is basically a simple HTTP Socket interface implementation. It looks similar to WebSocket while providing additional features and leveraging other transports when WebSocket is not supported by the user's browser.

这意味着从与 Socket.io 客户端/服务器交互的客户端/服务器代码的角度来看,它看起来类似于 WebSocket.但是,网络流量看起来与简单的 WebSocket 非常不同——一旦连接,除了建立在 WebSocket 之上的更强大的协议之外,还有一个初始握手.握手在这里和消息协议此处(两者都是指向 Socket.IO 协议规范的链接).

What this means is that it looks similar to WebSocket from the perspective of client/server code that interacts with the Socket.io client/server. However, the network traffic looks very different from a simple WebSocket - there's an initial handshake in addition to a more robust protocol built on top of WebSocket once that's connected. The handshake is described here and the message protocol here (both are links to the Socket.IO protocol spec).

如果您正在编写 WebSocket 服务器,除非您打算实现所有 Socket.io 协议,否则最好只使用裸 WebSocket 接口而不是 Socket.io 客户端.

If you're writing a WebSocket server, you're better off just using the bare WebSocket interface rather than the Socket.io client, unless you intend to implement all of the Socket.io protocol.

这篇关于我可以使用 socket.io-client 连接到标准的 websocket 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆