云州泇滴Go+Vue通用后台管理项目实战
2023-08-22 14:17 作者:bili_67158895267 | 我要投稿
创建WebSocket服务端程序
1、创建目录chat并初始化
cd chat
go mod init chat
2、增加文件client.go
,该文件处理WebSocket客户端程序。其中serveWebsocket()
函数处理客户端连接。
package mainimport (
"bytes"
"log"
"net/http"
"time"
"github.com/gorilla/websocket")const (
// Time allowed to write a message to the peer.
writeWait = 10 * time.Second // Time allowed to read the next pong message from the peer.
pongWait = 60 * time.Second // Send pings to peer with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10
// Maximum message size allowed from peer.
maxMessageSize = 512)var (
newline = []byte{'\n'}
space = []byte{' '})