Golang WebSocket客户端

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

Golang websocket client

问题

我想要建立客户端的WebSocket连接到外部服务器,每个连接都是一个goroutine和reader。我在互联网上找到了一些信息,但是我只找到了如何创建服务器端的WebSocket教程。
有没有人可以友好地给我提供一个简单的例子,并帮助我理解。我正在使用标准的golang库https://golang.org/x/net/websocket。

我写了一些代码,但是当我关闭一个连接时,程序会显示EOF信息然后退出。我不会贴出代码,因为它可能不好,因为这是我第一次尝试。

我知道如何从WebSocket读取/发送消息,但是我不知道如何创建多个连接。

如果有任何信息或者例子,我将非常感激。谢谢阅读。

英文:

I want to make client websocket connections to exertnal server each connection = goroutine and reader. I was looking informations on the internet but I found how to create server websocket tutorials.
Can anyone be so kind and make a trivial example and walk me through. I am using standart golang libary https://golang.org/x/net/websocket.

I created some code but when I closed one connection program exited with EOF information. I won't post the code because it's probably bad duo to the fact it was my first try.

I know how to read/send message from websocket but I don't know how to create multiple connections.

Any informations, examples would be appreciate, thanks for reading

答案1

得分: 12

你可以使用Gorilla WebSocket库

这是一个示例,展示了它作为客户端的使用方式。

英文:

You can use the Gorilla WebSocket library

Here's an example of it's use as a client

答案2

得分: 6

Golang官方文档建议使用gorilla来构建基于WebSocket的应用程序。然而,问题在于gorilla WebSocket不是基于事件的。应用程序需要处理并发的读写操作。开发者需要编写自定义的goroutines来处理连接、断开和读取事件。
我认为最好有一个库来处理所有这些事情。
因此,我决定在gorilla的基础上编写自己的客户端实现 - gowebsocket。你可以在这里找到更详细的解释:在Go中开始使用WebSocket客户端

英文:

Golang official doc recommends to use gorilla for building websocket based application. Still the problem is, gorilla websocket is not event based. Applications need to handle concurrent read and write operations. Developers need to write custom goroutines for handling connect, disconnect and read events.
I think it is better to have a library handling everything for you.
So, I decided to write down my own client implementation - gowebsocket on top of gorilla. You can find more detailed explaination here Getting started with websocket client in go

答案3

得分: 2

你可以在这个链接上查看给出的比较。

https://yalantis.com/uploads/ckeditor/pictures/4265/websocket-libraries.png

文章建议选择Gobwas(https://github.com/gobwas/ws)。从性能上来说,它是最好的选择,并提供了所有与WebSocket相关的应用所需的功能。

英文:

You can check the comparison given on this link.

https://yalantis.com/uploads/ckeditor/pictures/4265/websocket-libraries.png

Article suggests to go for Gobwas(https://github.com/gobwas/ws). Its best performance wise and offers all the required features needed for websockets related applications.

huangapple
  • 本文由 发表于 2015年11月26日 08:39:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/33928833.html
匿名

发表评论

匿名网友

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

确定