Browse Source

修正了移除WebSockect连接时发生的错误

pull/346/head
allanwei 3 years ago
parent
commit
ac4ec76d87
2 changed files with 30 additions and 20 deletions
  1. +24
    -16
      messaging/Contexts.go
  2. +6
    -4
      messaging/messaging.go

+ 24
- 16
messaging/Contexts.go View File

@ -6,43 +6,51 @@ import (
) )
type Contexts struct { type Contexts struct {
locker sync.RWMutex
connections map[string]*neffos.NSConn
callbacksOfAdd []func(string, *neffos.NSConn)
callbacksOfRemove []func(string)
authenticator func(map[string]string) (string, error)
locker sync.RWMutex
connectionOfContexts map[string]*neffos.NSConn
connectionId2ContextID map[string]string
callbacksOfAdd []func(string, *neffos.NSConn)
callbacksOfRemove []func(string)
authenticator func(map[string]string) (string, error)
} }
func newContexts() *Contexts { func newContexts() *Contexts {
return &Contexts{ return &Contexts{
connections: make(map[string]*neffos.NSConn, 10),
callbacksOfAdd: make([]func(string, *neffos.NSConn), 0, 10),
callbacksOfRemove: make([]func(string), 0, 10),
connectionOfContexts: make(map[string]*neffos.NSConn, 10),
connectionId2ContextID: make(map[string]string, 10),
callbacksOfAdd: make([]func(string, *neffos.NSConn), 0, 10),
callbacksOfRemove: make([]func(string), 0, 10),
} }
} }
func (contexts *Contexts) add(id string, connection *neffos.NSConn) {
func (contexts *Contexts) add(contextID string, connection *neffos.NSConn) {
locker.Lock() locker.Lock()
defer locker.Unlock() defer locker.Unlock()
contexts.connections[id] = connection
contexts.connectionOfContexts[contextID] = connection
contexts.connectionId2ContextID[connection.Conn.ID()] = contextID
for _, callback := range contexts.callbacksOfAdd { for _, callback := range contexts.callbacksOfAdd {
callback(id, connection)
callback(contextID, connection)
} }
} }
func (contexts *Contexts) remove(id string) {
func (contexts *Contexts) remove(connectionId string) {
locker.Lock() locker.Lock()
defer locker.Unlock() defer locker.Unlock()
delete(contexts.connections, id)
contextID, has := contexts.connectionId2ContextID[connectionId]
if !has {
return
}
delete(contexts.connectionId2ContextID, connectionId)
delete(contexts.connectionOfContexts, contextID)
for _, callback := range contexts.callbacksOfRemove { for _, callback := range contexts.callbacksOfRemove {
callback(id)
callback(contextID)
} }
} }
func (contexts *Contexts) Get(id string) *neffos.NSConn {
func (contexts *Contexts) Get(contextID string) *neffos.NSConn {
locker.RLock() locker.RLock()
defer locker.RUnlock() defer locker.RUnlock()
connection, ok := contexts.connections[id]
connection, ok := contexts.connectionOfContexts[contextID]
if ok { if ok {
return connection return connection
} else { } else {


+ 6
- 4
messaging/messaging.go View File

@ -55,16 +55,18 @@ func Register(namespaceName string, eventMapping neffos.Events) *Contexts {
} }
delete(headers, "token") delete(headers, "token")
fmt.Println(token) fmt.Println(token)
id := connection.Conn.ID() // Todo 需要根据 token 解析出 user id
contextID := connection.Conn.ID() // Todo 需要根据 token 解析出 user id
if contexts.authenticator != nil { if contexts.authenticator != nil {
var err error
id, err = contexts.authenticator(headers)
newContextID, err := contexts.authenticator(headers)
if err != nil { if err != nil {
connection.Emit("onLogin", wrapError(err.Error())) connection.Emit("onLogin", wrapError(err.Error()))
return nil return nil
} }
if newContextID != "" {
contextID = newContextID
}
} }
contexts.add(id, connection)
contexts.add(contextID, connection)
connection.Emit("onLogin", wrapError("")) connection.Emit("onLogin", wrapError(""))
return nil return nil
} }


Loading…
Cancel
Save