简介
使用到的包:github.com/gorilla/websocket
案例
package main import ( "fmt" "github.com/gorilla/websocket" "log" "net/url" "sync" "time" ) type websocketClientManager struct { conn *websocket.Conn addr *string path string sendMsgChan chan string recvMsgChan chan string isAlive bool timeout int } // 构造函数 func NewWsClientManager(addrIp, addrPort, path string, timeout int) *websocketClientManager { addrString := addrIp + ":" + addrPort var sendChan = make(chan string, 10) var recvChan = make(chan string, 10) var conn *websocket.Conn return &websocketClientManager{ addr: &addrString, path: path, conn: conn, sendMsgChan: sendChan, recvMsgChan: recvChan, isAlive: false, timeout: timeout, } } // 链接服务端 func (wsc *websocketClientManager) dail() { var err error u := url.URL{Scheme: "ws", Host: *wsc.addr, Path: wsc.path} log.Printf("connecting to %s", u.String()) wsc.conn, _, err = websocket.DefaultDialer.Dial(u.String(), nil) if err != nil { fmt.Println(err) return } wsc.isAlive = true log.Printf("connecting to %s 链接成功!!!", u.String()) } // 发送消息 func (wsc *websocketClientManager) sendMsgThread() { go func() { for { msg := <-wsc.sendMsgChan err := wsc.conn.WriteMessage(websocket.TextMessage, []byte(msg)) if err != nil { log.Println("write:", err) continue } } }() } // 读取消息 func (wsc *websocketClientManager) readMsgThread() { go func() { for { if wsc.conn != nil { _, message, err := wsc.conn.ReadMessage() if err != nil { log.Println("read:", err) wsc.isAlive = false // 出现错误,退出读取,尝试重连 break } log.Printf("recv: %s", message) // 需要读取数据,不然会阻塞 wsc.recvMsgChan <- string(message) } } }() } // 开启服务并重连 func (wsc *websocketClientManager) start() { for { if wsc.isAlive == false { wsc.dail() wsc.sendMsgThread() wsc.readMsgThread() } time.Sleep(time.Second * time.Duration(wsc.timeout)) } } func main() { wsc := NewWsClientManager("192.168.12.15", "10086", "/v1", 10) wsc.start() var w1 sync.WaitGroup w1.Add(1) w1.Wait() }
压测案例
package main import ( "fmt" "github.com/gorilla/websocket" "log" "net/url" "strconv" "time" ) const ( //根据自身情况修改服务器ip地址 serverAddr = "wss://im.aigupiao.com/ws/" ) func main() { succCount := 0 failCount := 0 for i := 1; i < 1001; i++ { u := url.URL{Scheme: "wss", Host: "im.aigupiao.com", Path: "/ws/" + strconv.Itoa(i)} log.Println(u.String()) conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil) if err != nil { log.Println(err) failCount++ fmt.Println("Number of dialing failures:", failCount) fmt.Println("Number of dialing fail:", err) time.Sleep(time.Second) continue } defer conn.Close() succCount++ fmt.Println("Number of successful connections:", succCount) go Client(conn, succCount) } //Stop here to prevent the program from exiting tick := time.NewTicker(10 * time.Second) for { select { case <-tick.C: } } } func Client(conn *websocket.Conn, index int) error { dataWrite := strconv.Itoa(index) + "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" + "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" + "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" + "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" + "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\n" for { conn.WriteMessage(websocket.TextMessage, []byte(dataWrite)) _, message, err := conn.ReadMessage() if err != nil { return err } log.Println(string(message)) time.Sleep(60 * time.Second) } return nil }
《本文》有 0 条评论