golang-im聊天
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

37 lines
638 B

  1. package connect
  2. import (
  3. "gim/pkg/pb"
  4. "sync"
  5. )
  6. var ConnsManager = sync.Map{}
  7. // SetConn 存储
  8. func SetConn(deviceId int64, conn *Conn) {
  9. ConnsManager.Store(deviceId, conn)
  10. }
  11. // GetConn 获取
  12. func GetConn(deviceId int64) *Conn {
  13. value, ok := ConnsManager.Load(deviceId)
  14. if ok {
  15. return value.(*Conn)
  16. }
  17. return nil
  18. }
  19. // DeleteConn 删除
  20. func DeleteConn(deviceId int64) {
  21. ConnsManager.Delete(deviceId)
  22. }
  23. // PushAll 全服推送
  24. func PushAll(message *pb.MessageSend) {
  25. ConnsManager.Range(func(key, value interface{}) bool {
  26. conn := value.(*Conn)
  27. conn.Send(pb.PackageType_PT_MESSAGE, 0, message, nil)
  28. return true
  29. })
  30. }