WebSocket与GopherJS的兼容性

huangapple go评论74阅读模式
英文:

WebSocket compatibility with GopherJS

问题

寻找一些关于如何在原生Go执行平台和通过GopherJS实现WebSocket客户端的一般指导。

看起来GopherJS有两个WebSocket库,一个是gopherjs/websocket,另一个是goxjs/websocket。似乎goxjs/websocket是“同构”的,意味着它应该在GopherJS中工作,并且与golang.org/x/net/websocket保持API兼容。这将提供两个执行环境的兼容性,这很好,但我不确定它是否是最佳解决方案。gopherjs/websocket似乎无法在浏览器执行环境之外工作,所以它是不可行的。

目前,对于原生Go应用程序,最推荐的WebSocket实现似乎是gorilla/websocket。我想使用它,因为它似乎有一个很好的API,并且似乎功能齐全。如果我导入gorilla/websocket,我可以成功运行gopherjs build,但我担心它在JavaScript环境中会失败,或者它会被编译成不使用浏览器的WebSocket API实现的JavaScript。

我没有看到很多关于哪个库适用于哪种情况的指导,所以希望这可以帮助比较和对比,并提供选择合适的方向。

英文:

Looking for some general guidance on how to implement a WebSocket client in a way that works on both native Go execution platforms and through GopherJS.

It appears that GopherJS has two WebSocket libraries, one at gopherjs/websocket and one at goxjs/websocket. It seems that goxjs/websocket is "isomorphic", meaning it should work in GopherJS while remaining API-compatible with golang.org/x/net/websocket. That would provide compatibility in both execution environments, which is fine, but I'm not sure if it's the best solution. gopherjs/websocket doesn't appear to work outside of the browser execution environment, so it's a non-starter.

The most recommended WebSocket implementation for native Go apps seems to be gorilla/websocket at the moment. I'd like to use this as it seems to have a good API and it seems rather fully-featured. I can successfully run gopherjs build if I import gorilla/websocket, but I'm concerned that it will fail in the JavaScript environment, or that it will be compiled down into JavaScript that doesn't use the browser's implementation of the WebSocket API.

I'm not seeing much guidance on which library is appropriate for which scenario, so hoping this can help compare and contrast and provide the direction necessary to make a good choice.

答案1

得分: 1

我没有关于你应该做什么的指导,但可以分享以下事实:

gorilla/websocket包是在TCP连接上实现Websocket协议的一个包。因为浏览器应用程序无法直接访问TCP连接,所以gorilla/websocket包无法在浏览器中运行。

gorilla/websocket包和gopherjs/websocket包在提供的模型上有所区别。WebSocket协议是基于消息的,gorilla/websocket包在消息周围提供了一个API。gopherjs/websocket提供了绑定,使基于消息的WebSocket协议看起来像是一串字节流。gopherjs/websocket API中没有暴露消息。

x/net/websocket包几乎被废弃了。存在着长期存在的与协议兼容性相关的问题,这些问题尚未得到解决。

英文:

I don't have any guidance on what you should do, but can share these facts:

The gorilla/websocket package is an implementation of the Websocket protocol on TCP connections. Because browser applications cannot access TCP connections directly, the gorilla/websocket package will not run in the browser.

There's a difference in the models presented by gorilla/websocket package and the gopherjs/websocket packages. The WebSocket protocol is message-based and the gorilla/websocket package exposes an API around messages. The gopherjs/websocket provides bindings that make the message-based WebSocket protocol look like a stream of bytes. Messages are not exposed in the gopherjs/websocket API.

The x/net/websocket package is all but abandoned. There are long standing issues with protocol compliance that have not been addressed.

huangapple
  • 本文由 发表于 2016年10月24日 14:42:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/40212148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定