From 7f5fdca0cfb37754f3dc4c37a2cf813e838470c2 Mon Sep 17 00:00:00 2001 From: boyce Date: Fri, 22 Mar 2019 18:23:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B4=E7=90=86=E6=97=A5=E5=BF=97=E4=B8=8Epi?= =?UTF-8?q?ng=E8=87=AA=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- network/websocketclient.go | 17 +++++++++++------ network/websocketserver.go | 2 -- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/network/websocketclient.go b/network/websocketclient.go index b257c13..78456b7 100644 --- a/network/websocketclient.go +++ b/network/websocketclient.go @@ -16,7 +16,7 @@ import ( //IWebsocketClient ... type IWebsocketClient interface { - Init(slf IWebsocketClient, strurl, strPath string, bproxy bool, timeoutsec time.Duration) error + Init(slf IWebsocketClient, strurl, strProxyPath string, timeoutsec time.Duration) error Start() error WriteMessage(msg []byte) error OnDisconnect() error @@ -36,16 +36,17 @@ type WebsocketClient struct { timeoutsec time.Duration bRun bool + ping string } //Init ... -func (ws *WebsocketClient) Init(slf IWebsocketClient, strurl, strPath string, bproxy bool, timeoutsec time.Duration) error { +func (ws *WebsocketClient) Init(slf IWebsocketClient, strurl, strProxyPath string, timeoutsec time.Duration) error { ws.timeoutsec = timeoutsec ws.slf = slf - if bproxy == true { + if strProxyPath != "" { proxy := func(_ *http.Request) (*url.URL, error) { - return url.Parse(strPath) + return url.Parse(strProxyPath) } if timeoutsec > 0 { @@ -65,10 +66,14 @@ func (ws *WebsocketClient) Init(slf IWebsocketClient, strurl, strPath string, bp ws.url = strurl ws.bwritemsg = make(chan []byte, 1000) - + ws.ping = `ping` return nil } +func (ws *WebsocketClient) SetPing(ping string) { + ws.ping = ping +} + //OnRun ... func (ws *WebsocketClient) OnRun() error { for { @@ -145,7 +150,7 @@ func (ws *WebsocketClient) writeMsg() error { select { case <-timerC: if ws.state == 2 { - ws.WriteMessage([]byte(`ping`)) + ws.WriteMessage([]byte(ws.ping)) } case msg := <-ws.bwritemsg: if ws.state == 2 { diff --git a/network/websocketserver.go b/network/websocketserver.go index 8a28b7e..ffb8113 100644 --- a/network/websocketserver.go +++ b/network/websocketserver.go @@ -121,7 +121,6 @@ func (slf *WebsocketServer) startListen() { if err != nil { service.GetLogger().Printf(sysmodule.LEVER_FATAL, "http.ListenAndServe(%d, nil) error:%v\n", slf.port, err) - fmt.Printf("http.ListenAndServe(%d, nil) error\n", slf.port) os.Exit(1) } } @@ -159,7 +158,6 @@ func (slf *WebsocketServer) Disconnect(clientid uint64) { } value.conn.Close() - //slf.ReleaseClient(value) } func (slf *WebsocketServer) Stop() {