Websocket控制消息

时间:2018-01-10 06:07:35

标签: go gorilla

我在Golang服务器和客户端上有两个项目。

问题是当我从服务器发送controll消息时,我无法在客户端按类型获取它。

很少有服务器代码示例:

发送PingMessage:

-----> Node.js app detected
-----> Creating runtime environment

   NPM_CONFIG_LOGLEVEL=error
   NPM_CONFIG_PRODUCTION=true
   NODE_VERBOSE=false
   NODE_ENV=production
   NODE_MODULES_CACHE=true
-----> Installing binaries
   engines.node (package.json):  4.1.1
   engines.npm (package.json):   unspecified (use default)

   Resolving node version 4.1.1...
   Downloading and installing node 4.1.1...
   Using default npm version: 2.14.4
-----> Restoring cache
   Skipping cache restore (not-found)
-----> Building dependencies
   Installing node modules (package.json)
   npm ERR! install Couldn't read dependencies
   npm ERR! Linux 4.14.12-041412-generic
   npm ERR! argv "/tmp/build_fd784069b3690d9546dfaea15e0e3251/digracesion-make-sandwich-0fe3e02/.heroku/node/bin/node" "/tmp/build_fd784069b3690d9546dfaea15e0e3251/digracesion-make-sandwich-0fe3e02/.heroku/node/bin/npm" "install" "--unsafe-perm" "--userconfig" "/tmp/build_fd784069b3690d9546dfaea15e0e3251/digracesion-make-sandwich-0fe3e02/.npmrc"
   npm ERR! node v4.1.1
   npm ERR! npm  v2.14.4

   npm ERR! Invalid name: "Make Sandwich App"
   npm ERR!
   npm ERR! If you need help, you may report this error at:
   npm ERR!     <https://github.com/npm/npm/issues>

   npm ERR! Please include the following file with any support request:
   npm ERR!     /tmp/build_fd784069b3690d9546dfaea15e0e3251/digracesion-make-sandwich-0fe3e02/npm-debug.log
-----> Build failed

发送CloseMessage:

ws.SetWriteDeadline(time.Now().Add(10 * time.Second))
ws.WriteMessage(websocket.PingMessage, new_msg)

客户方:

ws.WriteControl(websocket.CloseMessage,
    websocket.FormatCloseMessage(websocket.CloseNormalClosure, "socket close"),
        time.Now().Add(3 * time.Second))

例如CloseNormalClosure消息我只能得到:

for{
    t, socketMsg, err := ws.ReadMessage()


    if websocket.IsUnexpectedCloseError(err) {
        webSock.keepLive()
    }

    switch t {
    case websocket.CloseNormalClosure:
        webSock.keepLive()

    case websocket.PingMessage:
        log.Warn("get ping!!!")

    case websocket.TextMessage:
        SocketChannel <- socketMsg
    }


}

但PingMessage,我无法按类型获取:

    if websocket.IsCloseError(err, websocket.CloseNormalClosure){
        log.Warn("CloseNormalClosure message")
    }

你能帮帮我吗,我做错了什么?

1 个答案:

答案 0 :(得分:2)

documentation says

  

Connections句柄通过调用使用SetCloseHandler方法设置的处理函数并从NextReader,ReadMessage或消息Read方法返回* CloseError来接收关闭消息。默认关闭处理程序向对等方发送关闭消息。

     

Connections句柄通过调用使用SetPingHandler方法设置的处理函数来接收ping消息。默认的ping处理程序向对等方发送pong消息。

     

Connections句柄通过调用使用SetPongHandler方法设置的处理函数来接收pong消息。默认的pong处理程序什么都不做。如果应用程序发送ping消息,则应用程序应设置pong处理程序以接收相应的pong。

将上面的代码写为:

=SUMPRODUCT((B2:B2000<>'')/COUNTIF(B2:D2000,B2:B2000&''))

大多数应用程序在出现任何错误时都会中断接收循环。更典型的方法是将上面的代码编写为:

ws.SetPingHandler(func(s string) error {
   log.Warn("get ping!!!")
   return nil
})

for {
    t, socketMsg, err := ws.ReadMessage()
    switch {
    case websocket.IsCloseError(websocket.CloseNormalClosure):
        webSock.keepLive()
    case websocket.IsUnexpectedCloseError(err):
        webSock.keepLive()
    case t == websocket.TextMessage:
        SocketChannel <- socketMsg
    }
}