我正在使用 Go 中的 websockets 编写一个聊天应用程序。
会有多个聊天室,想法是将所有连接到聊天室的 websocket 存储在 Redis 列表中。
为了在 Redis 中存储和检索 websocket,我必须对它们进行编码/解码,并且(在这个问题之后)我认为我可以使用 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
还是需要类型转换?