整理日志与ping自定义

This commit is contained in:
boyce
2019-03-22 18:23:29 +08:00
parent 9e75434152
commit 7f5fdca0cf
2 changed files with 11 additions and 8 deletions

View File

@@ -16,7 +16,7 @@ import (
//IWebsocketClient ... //IWebsocketClient ...
type IWebsocketClient interface { 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 Start() error
WriteMessage(msg []byte) error WriteMessage(msg []byte) error
OnDisconnect() error OnDisconnect() error
@@ -36,16 +36,17 @@ type WebsocketClient struct {
timeoutsec time.Duration timeoutsec time.Duration
bRun bool bRun bool
ping string
} }
//Init ... //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.timeoutsec = timeoutsec
ws.slf = slf ws.slf = slf
if bproxy == true { if strProxyPath != "" {
proxy := func(_ *http.Request) (*url.URL, error) { proxy := func(_ *http.Request) (*url.URL, error) {
return url.Parse(strPath) return url.Parse(strProxyPath)
} }
if timeoutsec > 0 { if timeoutsec > 0 {
@@ -65,10 +66,14 @@ func (ws *WebsocketClient) Init(slf IWebsocketClient, strurl, strPath string, bp
ws.url = strurl ws.url = strurl
ws.bwritemsg = make(chan []byte, 1000) ws.bwritemsg = make(chan []byte, 1000)
ws.ping = `ping`
return nil return nil
} }
func (ws *WebsocketClient) SetPing(ping string) {
ws.ping = ping
}
//OnRun ... //OnRun ...
func (ws *WebsocketClient) OnRun() error { func (ws *WebsocketClient) OnRun() error {
for { for {
@@ -145,7 +150,7 @@ func (ws *WebsocketClient) writeMsg() error {
select { select {
case <-timerC: case <-timerC:
if ws.state == 2 { if ws.state == 2 {
ws.WriteMessage([]byte(`ping`)) ws.WriteMessage([]byte(ws.ping))
} }
case msg := <-ws.bwritemsg: case msg := <-ws.bwritemsg:
if ws.state == 2 { if ws.state == 2 {

View File

@@ -121,7 +121,6 @@ func (slf *WebsocketServer) startListen() {
if err != nil { if err != nil {
service.GetLogger().Printf(sysmodule.LEVER_FATAL, "http.ListenAndServe(%d, nil) error:%v\n", slf.port, err) 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) os.Exit(1)
} }
} }
@@ -159,7 +158,6 @@ func (slf *WebsocketServer) Disconnect(clientid uint64) {
} }
value.conn.Close() value.conn.Close()
//slf.ReleaseClient(value)
} }
func (slf *WebsocketServer) Stop() { func (slf *WebsocketServer) Stop() {