介绍
本篇文章为大家展示了使用golang在实现一个websocket服务端,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
创建一个websocket的服务端
package 微笑 import ( “才能errors" “才能log" “;net/http"才能; “才能sync" “才能time" “;github.com/gorilla/websocket"才能; ) const (//才能,允许等待的写入时间 时间=writeWait 才能;10,* time.Second//才能,Time allowed 用read 从而next pong message 得到,同行。 时间=pongWait 才能;60,* time.Second//才能,Send pings 用peer with 却;能够时期只Must be less than pongWait。 pingPeriod 才能=,(pongWait *, 9),/10//才能,Maximum message size allowed 得到同行。 maxMessageSize 才能=512 )//,最大的连接ID,每次连接都加1,处理 var maxConnId  int64//,客户端读写消息 type wsMessage  struct {//才能,websocket.TextMessage 消息类型 messageType 才能,int data 才能,,,[]字节 }//,ws 的所有连接//,用于广播 var wsConnAll [int64] * wsConnection地图 var upgrader =, websocket.Upgrader { 1024年,ReadBufferSize:大敌; 1024年,WriteBufferSize:大敌;//才能,允许所有的CORS 跨域请求,正式环境可以关闭 ,,CheckOrigin: func (r * http.Request), bool { ,,,return 真实的 ,,}, }//,客户端连接 type wsConnection  struct { wsSocket 才能;* websocket.Conn //,底层websocket ,,inChan chan * wsMessage //,读队列 outChan 才能;chan * wsMessage //,写队列 mutex 才能,,,sync.Mutex //,避免重复关闭管道,加锁处理 ,isClosed 保龄球 closeChan 才能;chan  byte //,关闭通知 id 才能,,,int64 } func wsHandler (resp http.ResponseWriter, req * http.Request), {//才能,应答客户端告知升级连接为websocket wsSocket,才能,err :=, upgrader.Upgrade(职责,要求,,nil) if 才能;err  !=, nil { ,,,log.Println(“升级为websocket失败“,,err.Error ()) ,才能返回 ,,} ,maxConnId + +//才能,TODO 如果要控制连接数可以计算,wsConnAll长度//才能,连接数保持一定数量,超过的部分不提供服务 wsConn 才能;:=,,wsConnection { ,,,wsSocket:, wsSocket, ,,,inChan:,,使(chan * wsMessage, 1000), ,,,outChan:,,使(chan * wsMessage, 1000), ,,,closeChan:, (chan 字节), ,,,的空当:,假的, ,,,,,,,,maxConnId, ,,} wsConnAll才能[maxConnId],=wsConn log.Println才能(“当前在线人数“,,len (wsConnAll))//才能,处理器,发送定时信息,避免意外关闭 go 才能;wsConn.processLoop ()//,才能读协程 go 才能;wsConn.wsReadLoop ()//才能,写协程 go 才能;wsConn.wsWriteLoop () }//,处理队列中的消息 func (wsConn * wsConnection), processLoop (), {//才能,处理消息队列中的消息//,才能获取到消息队列中的消息,处理完成后,发送消息给客户端 for {才能 ,,,,err 味精:=,wsConn.wsRead () ,,,if err !=, nil { ,,,,,log.Println(“获取消息出现错误,,,err.Error ()) ,,,,,休息 ,,,} ,,,log.Println(“接收到消息,,,字符串(msg.data)) ,,,//,修改以下内容把客户端传递的消息传递给处理程序 ,,,err =, wsConn.wsWrite (msg.messageType, msg.data) ,,,if err !=, nil { ,,,,,log.Println(“发送消息给客户端出现错误,,,err.Error ()) ,,,,,休息 ,,,} ,,} }//,处理消息队列中的消息 func (wsConn * wsConnection), wsReadLoop (), {//才能,设置消息的最大长度 wsConn.wsSocket.SetReadLimit才能(maxMessageSize) 阀门wsConn.wsSocket.SetReadDeadline才能(time.Now () (pongWait)) for {才能 ,,,//,读一个消息 ,,,msgType,,, err 数据:=,wsConn.wsSocket.ReadMessage () ,,,if err !=, nil { ,,,,,websocket.IsUnexpectedCloseError (websocket.CloseGoingAway,呃,还以为;websocket.CloseAbnormalClosure) ,,,,,log.Println(“消息读取出现错误,,,err.Error ()) ,,,,,wsConn.close () null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null使用golang在实现一个websocket服务端