go语言聊天室实现(七)websocket收消息设置

Python017

go语言聊天室实现(七)websocket收消息设置,第1张

上一节中,我们为每个连接都创建了一个goroutine来读取其中的消息,现在我们将这个读取消息的方法实现一下。

我们在application目录下新建controllers目录,并在其中创建一个MessageController.go文件。

首先我们新建一个MessageController的结构体,内容如下

这个结构体包括两个内容,一个是我们将连接放在数组之后,返回的索引,另一个是连接本身.

这个是具体的方法。

我们首先设置了一下读消息的大小、超时时间以及超时后需要的操作。

超时时间如果设置为0,那么就是永不超时。之前在这里直接写0,被告知需要传一个time.Time类型的数据。最终谷歌后才得到了这个值time.Time{}为"0001-01-01 00:00:00 +0000 UTC"。

我们将用户手法消息的内容定义为一个结构体,然后将用户的订阅信息的json通过json.unmarshal转换成这个结构体。

之后的switch操作与我们在Swoole中的操作基本雷同,在查询到login之后,调用service中 的login方法来进行注册。

下一节中我们再介绍具体的注册逻辑。

1.在创建连接池之后,起一个 go routine,每隔一段 idleTime 发送一个 PING 到 Redis server。其中,idleTime 略小于 Redis server 的 timeout 配置。

2.连接池初始化部分代码如下:

p, err := pool.New("tcp", u.Host, concurrency) errHndlr(err) go func() { for { p.Cmd("PING") time.Sleep(idelTime * time.Second) } }()

3.使用 redis 传输数据部分代码如下:

func redisDo(p *pool.Pool, cmd string, args ...interface{}) (reply *redis.Resp, err error) { reply = p.Cmd(cmd, args...) if err = reply.Errerr != nil { if err != io.EOF { Fatal.Println("redis", cmd, args, "err is", err) } } return }

4.其中,Radix.v2 连接池内部进行了连接池内连接的获取和放回,代码如下:

// Cmd automatically gets one client from the pool, executes the given command // (returning its result), and puts the client back in the pool func (p *Pool) Cmd(cmd string, args ...interface{}) *redis.Resp { c, err := p.Get() if err != nil { return redis.NewResp(err) } defer p.Put(c) return c.Cmd(cmd, args...) }

这样,就有了系统 keep alive 的机制,不会出现 time out 的连接了,从 redis 连接池里面取出的连接都是可用的连接了。看似简单的代码,却完美的解决了连接池里面超时连接的问题。同时,就算 Redis server 重启等情况,也能保证连接自动重连。