func main() {
wsc := NewWsClientManager("127.0.0.1", "8100", "", 10)
wsc.dail()
}
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: "wss", Host: *wsc.addr, Path: wsc.path}
log.Printf("connecting to %s", u.String())
dialer := websocket.Dialer{TLSClientConfig: &tls.Config{RootCAs: nil, InsecureSkipVerify: true}}
wsc.conn, _, err = dialer.Dial(u.String(), nil)
if err != nil {
fmt.Println(err)
return
}
wsc.isAlive = true
log.Printf("connecting to %s 链接成功!!!", u.String())
wsc.conn.Close()
// for 会不断接收服务器端的数据
for {
// 接收消息
_, p, err := wsc.conn.ReadMessage()
if err != nil {
}
reqParam := ReceiveA{}
err = json.Unmarshal(p, &reqParam)
if err != nil {
}
//每次接到数据在此对数据进行处理
........//处理方法
}
}
07-29
2212
09-29
678