...
|
...
|
@@ -17,12 +17,12 @@ const ( |
|
|
Connected
|
|
|
)
|
|
|
|
|
|
func init() {
|
|
|
keyType := reflect.TypeOf(&websocket.Conn{})
|
|
|
valueType := reflect.TypeOf(&WebsocketConnection{})
|
|
|
Connections = NewJMap(keyType, valueType)
|
|
|
Clients = NewJMap(reflect.TypeOf("1:1"), valueType)
|
|
|
}
|
|
|
//func init() {
|
|
|
// keyType := reflect.TypeOf(&websocket.Conn{})
|
|
|
// valueType := reflect.TypeOf(&WebsocketConnection{})
|
|
|
// Connections = NewJMap(keyType, valueType)
|
|
|
// Clients = NewJMap(reflect.TypeOf("1:1"), valueType)
|
|
|
//}
|
|
|
|
|
|
type ReceiveHandler (func([]byte) *mybeego.Message)
|
|
|
|
...
|
...
|
@@ -35,6 +35,7 @@ type WebsocketConnection struct { |
|
|
State ConnState
|
|
|
OnReceive ReceiveHandler
|
|
|
OnceClose sync.Once
|
|
|
Connmgrg *Connmgrs
|
|
|
}
|
|
|
|
|
|
func NewWebsocketConnection(conn *websocket.Conn,head *mybeego.RequestHead,recv ReceiveHandler)*WebsocketConnection{
|
...
|
...
|
@@ -46,14 +47,15 @@ func NewWebsocketConnection(conn *websocket.Conn,head *mybeego.RequestHead,recv |
|
|
Wchan: make(chan string, 10),
|
|
|
State: Connected,
|
|
|
OnReceive: recv,
|
|
|
Connmgrg:&DefaultConnmgrs,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//声明了两个cliets 管理 一个通过uid 一个通过conn管理
|
|
|
// key(*websocket.Conn) value(*WebsocketConnection)
|
|
|
var Connections *JMap
|
|
|
//var Connections *JMap
|
|
|
// key=uid(int64) value(*WebsocketConnection)
|
|
|
var Clients *JMap
|
|
|
//var Clients *JMap
|
|
|
|
|
|
type JMap struct {
|
|
|
sync.RWMutex
|
...
|
...
|
@@ -107,13 +109,12 @@ func (this *JMap) Put(k interface{}, v interface{}) bool { |
|
|
if !this.acceptable(k, v) {
|
|
|
return false
|
|
|
}
|
|
|
if connI, ok := Clients.Get(k); ok {
|
|
|
if connI, ok := this.m[k]; ok {
|
|
|
beego.Debug("key:", k, "已经连接,先剔除下线")
|
|
|
if conn, ok := connI.(*WebsocketConnection); ok {
|
|
|
//conn.Conn.WriteMessage(websocket.TextMessage, []byte("您的帐号在其它地方登录,您被剔除下线"))
|
|
|
conn.Close()
|
|
|
}
|
|
|
|
|
|
}
|
|
|
this.Lock()
|
|
|
this.m[k] = v
|
...
|
...
|
@@ -152,14 +153,18 @@ func (this *JMap) Contains(k interface{}) bool { |
|
|
|
|
|
func (c *WebsocketConnection) Serve() {
|
|
|
c.State = Connected
|
|
|
Connections.Put(c.Conn, c)
|
|
|
key := fmt.Sprintf("%d:%d", c.Uid, c.AppId)
|
|
|
Clients.Put(key, c)
|
|
|
|
|
|
//Connections.Put(c.Conn, c)
|
|
|
//key := fmt.Sprintf("%d:%d", c.Uid, c.AppId)
|
|
|
//Clients.Put(key, c)
|
|
|
c.Connmgrg.Put(c.GetConnmgrId(c.Uid),c,c)
|
|
|
go doWrite(c)
|
|
|
doRead(c)
|
|
|
}
|
|
|
|
|
|
func(c *WebsocketConnection)GetConnmgrId(uid int64)int{
|
|
|
return (int)(uid % int64(len(*c.Connmgrg)))
|
|
|
}
|
|
|
|
|
|
func (c *WebsocketConnection) Send(msg string) {
|
|
|
//panic("panic in websocket.send...")
|
|
|
c.Wchan <- msg
|
...
|
...
|
@@ -167,14 +172,15 @@ func (c *WebsocketConnection) Send(msg string) { |
|
|
|
|
|
func (c *WebsocketConnection) Close() {
|
|
|
c.OnceClose.Do(func(){
|
|
|
beego.Info("ws:close----uid:", c.Uid, "appid:", c.AppId, "state:", c.State)
|
|
|
beego.Info("ws:close----uid:", c.Uid, "appid:", c.AppId, "state:", c.State ,"connmgr-id:",c.GetConnmgrId(c.Uid))
|
|
|
if c.State == Disconnected {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
Connections.Remove(c.Conn)
|
|
|
key := fmt.Sprintf("%d:%d", c.Uid, c.AppId)
|
|
|
Clients.Remove(key)
|
|
|
//Connections.Remove(c.Conn)
|
|
|
//key := fmt.Sprintf("%d:%d", c.Uid, c.AppId)
|
|
|
//Clients.Remove(key)
|
|
|
c.Connmgrg.Remove(c.GetConnmgrId(c.Uid),c)
|
|
|
|
|
|
c.State = Disconnected
|
|
|
close(c.Echan)
|
...
|
...
|
@@ -236,27 +242,27 @@ func doWrite(c *WebsocketConnection) { |
|
|
}
|
|
|
}
|
|
|
|
|
|
func SendDataByWs(uid int64, appId int, sendMsg interface{}) bool {
|
|
|
if sendMsg == nil || uid < 1 || appId < 1 {
|
|
|
return false
|
|
|
}
|
|
|
msg := &mybeego.Message{
|
|
|
Errno: 0,
|
|
|
Errmsg: mybeego.NewMessage(0).Errmsg,
|
|
|
Data: sendMsg,
|
|
|
}
|
|
|
msgByte, err := json.Marshal(msg)
|
|
|
if err != nil {
|
|
|
beego.Error(err)
|
|
|
return false
|
|
|
}
|
|
|
key := fmt.Sprintf("%d:%d", uid, appId)
|
|
|
if connI, ok := Clients.Get(key); ok {
|
|
|
beego.Debug(ok)
|
|
|
if conn, ok := connI.(*WebsocketConnection); ok {
|
|
|
conn.Send(string(msgByte))
|
|
|
return true
|
|
|
}
|
|
|
}
|
|
|
return false
|
|
|
} |
|
|
//func SendDataByWs(uid int64, appId int, sendMsg interface{}) bool {
|
|
|
// if sendMsg == nil || uid < 1 || appId < 1 {
|
|
|
// return false
|
|
|
// }
|
|
|
// msg := &mybeego.Message{
|
|
|
// Errno: 0,
|
|
|
// Errmsg: mybeego.NewMessage(0).Errmsg,
|
|
|
// Data: sendMsg,
|
|
|
// }
|
|
|
// msgByte, err := json.Marshal(msg)
|
|
|
// if err != nil {
|
|
|
// beego.Error(err)
|
|
|
// return false
|
|
|
// }
|
|
|
// key := fmt.Sprintf("%d:%d", uid, appId)
|
|
|
// if connI, ok := Clients.Get(key); ok {
|
|
|
// beego.Debug(ok)
|
|
|
// if conn, ok := connI.(*WebsocketConnection); ok {
|
|
|
// conn.Send(string(msgByte))
|
|
|
// return true
|
|
|
// }
|
|
|
// }
|
|
|
// return false
|
|
|
//} |
...
|
...
|
|