使用Go语言中的gob对Websockets进行编码

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

Encode websockets in Go with gob

问题

我正在使用Go语言编写一个使用Websockets的聊天应用程序。

应用程序中会有多个聊天室,我的想法是将连接到聊天室的所有Websockets存储在Redis列表中。

为了在Redis中存储和检索Websockets,我需要对它们进行编码/解码,根据这个问题的建议,我认为可以使用gob来实现。

我在Redis中使用github.com/garyburd/redigo/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进行编码的整体思路错误了,还是需要进行类型转换?

英文:

I am writing a chat application using websockets in Go.

There will be multiple chat rooms and the idea is to store all the websockets connected to a chat room in a Redis list.

In order to store and retrieve the websockets in Redis I have to encode/decode them and (following this question) I thought that I can use gob for that.

I am using github.com/garyburd/redigo/redis for Redis and github.com/gorilla/websocket as my websocket library.

My function looks like:

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())
	}
}

However, I am getting this 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)

What does this error mean? Is the whole idea of encoding the *websocket.Conn wrong or type conversion is required?

答案1

得分: 1

根据文档的详细说明,gob.NewEncoder的参数是你希望将编码结果写入的io.Writer。它返回一个编码器,你需要将要编码的对象传递给它。它将对对象进行编码并将结果写入写入器。

假设conn是你的Redis连接,你可以使用以下代码:

buff := new(bytes.Buffer)
err := gob.NewEncoder(buff).Encode(ws)
if err != nil {
    // 处理错误
}
_, err := conn.Do("RPUSH", room, buff.Bytes())

这段代码将ws对象编码并将结果写入buff。然后,它使用Redis的RPUSH命令将buff的字节表示作为值推送到名为room的列表中。

英文:

As detailed in the documentation, argument to gob.NewEncoder is the io.Writer you want the encoded result written to. This returns an encoder, to which you pass the object you want encoded. It will encode the object and write the result to the writer.

Assuming that conn is your redis connection, you want something like:

buff := new(bytes.Buffer)
err := gob.NewEncoder(buff).Encode(ws)
if err != nil {
    // handle error
}
_,err := conn.Do("RPUSH", room, buff.Bytes())

huangapple
  • 本文由 发表于 2017年8月29日 23:49:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/45943414.html
匿名

发表评论

匿名网友

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

确定