云州泇滴Go+Vue通用后臺(tái)管理項(xiàng)目實(shí)戰(zhàn)
2023-08-22 14:17 作者:bili_67158895267 | 我要投稿
創(chuàng)建WebSocket服務(wù)端程序
1、創(chuàng)建目錄chat并初始化
cd chat
go mod init chat
2、增加文件client.go
,該文件處理WebSocket客戶端程序。其中serveWebsocket()
函數(shù)處理客戶端連接。
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{' '})
標(biāo)簽: