You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.2 KiB
72 lines
1.2 KiB
|
1 year ago
|
package webstocetmsg
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/gorilla/websocket"
|
||
|
|
)
|
||
|
|
|
||
|
|
/*
|
||
|
|
*
|
||
|
|
@ 作者: 秦东
|
||
|
|
@ 时间: 2024-08-16 15:57:05
|
||
|
|
@ 功能: 读取消息.Read 方法比较简单,从终端接收请求消息后,消息实例通过 WebSocket 回应接收消息状态,并不返回请求结果。结果通过 Write 方法返回。
|
||
|
|
@ 参数
|
||
|
|
|
||
|
|
#
|
||
|
|
|
||
|
|
@ 返回值
|
||
|
|
|
||
|
|
#
|
||
|
|
|
||
|
|
@ 方法原型
|
||
|
|
|
||
|
|
#
|
||
|
|
*/
|
||
|
|
func (c *Client) Read(close, renewal chan *Client) {
|
||
|
|
defer func() {
|
||
|
|
close <- c
|
||
|
|
}()
|
||
|
|
for {
|
||
|
|
_, message, err := c.Socket.ReadMessage()
|
||
|
|
if err != nil {
|
||
|
|
fmt.Printf("错误信息(1):%v\n", err)
|
||
|
|
break
|
||
|
|
}
|
||
|
|
fmt.Printf("获取的消息:%v\n", message)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
*
|
||
|
|
@ 作者: 秦东
|
||
|
|
@ 时间: 2024-08-16 16:06:47
|
||
|
|
@ 功能: Write 方法将请求结果返回给终端。Client 会监听 send channel,当 channel 有数据时,通过 socket 连接将消息发送给终端。
|
||
|
|
@ 参数
|
||
|
|
|
||
|
|
#
|
||
|
|
|
||
|
|
@ 返回值
|
||
|
|
|
||
|
|
#
|
||
|
|
|
||
|
|
@ 方法原型
|
||
|
|
|
||
|
|
#
|
||
|
|
*/
|
||
|
|
func (c *Client) Write(close chan *Client) {
|
||
|
|
for {
|
||
|
|
select {
|
||
|
|
case message, ok := <-c.Send:
|
||
|
|
if !ok {
|
||
|
|
fmt.Printf("错误信息(2):%v\n", ok)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.Socket.WriteMessage(websocket.TextMessage, message)
|
||
|
|
default:
|
||
|
|
fmt.Printf("错误信息(3):%v\n", close)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|