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

查看:2056
本文介绍了我可以使用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}"
)

发生,因此不会触发事件上的,代码立即退出。我试过:离开端口离开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服务器,你最好只使用裸露的WebSocket接口而不是Socket.io客户端,除非你打算实现所有的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天全站免登陆