|
@@ -31,8 +31,6 @@ func init() {
|
|
|
func send(conn *websocket.Conn, uid int64) {
|
|
|
pip := Conns[uid].pip
|
|
|
defer func() {
|
|
|
- close(pip)
|
|
|
- conn.Close()
|
|
|
delete(Conns, uid)
|
|
|
}()
|
|
|
for {
|
|
@@ -42,20 +40,17 @@ func send(conn *websocket.Conn, uid int64) {
|
|
|
fmt.Println("link closed")
|
|
|
break
|
|
|
}
|
|
|
- err = conn.WriteMessage(1, msg)
|
|
|
+ err = conn.WriteMessage(data.MsgType, msg)
|
|
|
if err != nil {
|
|
|
fmt.Println(err.Error())
|
|
|
- continue
|
|
|
+ break
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 接收消息
|
|
|
func reception(conn *websocket.Conn, uid int64) {
|
|
|
- pip := Conns[uid].pip
|
|
|
defer func() {
|
|
|
- close(pip)
|
|
|
- conn.Close()
|
|
|
delete(Conns, uid)
|
|
|
}()
|
|
|
for {
|
|
@@ -65,7 +60,11 @@ func reception(conn *websocket.Conn, uid int64) {
|
|
|
fmt.Println(err.Error())
|
|
|
break
|
|
|
}
|
|
|
- Conns[msg.To].pip <- &msg
|
|
|
+ if v, ok := Conns[msg.To]; ok {
|
|
|
+ v.pip <- &msg
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|