【发布时间】:2017-08-29 15:49:12
【问题描述】:
我正在用 Go 语言编写一个使用 websockets 的聊天应用程序。
会有多个聊天室,想法是将所有连接到聊天室的 websocket 存储在 Redis 列表中。
为了在 Redis 中存储和检索 websocket,我必须对它们进行编码/解码,并且(在this 问题之后)我认为我可以使用 gob。
我将 github.com/garyburd/redigo/redis 用于 Redis,github.com/gorilla/websocket 作为我的 websocket 库。
我的功能如下:
func addWebsocket(room string, ws *websocket.Conn) {
conn := pool.Get()
defer conn.Close()
enc := gob.NewEncoder(ws)
_, err := conn.Do("RPUSH", room, enc)
if err != nil {
panic(err.Error())
}
}
但是,我收到此错误:
cannot use ws (type *websocket.Conn) as type io.Writer in argument to gob.NewEncoder:
*websocket.Conn does not implement io.Writer (missing Write method)
have websocket.write(int, time.Time, ...[]byte) error
want Write([]byte) (int, error)
这个错误是什么意思?编码*websocket.Conn 的整个想法是错误的还是需要类型转换?
【问题讨论】:
标签: go websocket gorilla redigo