-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package wcfrest | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
|
||
"github.com/gorilla/websocket" | ||
"github.com/opentdp/go-helper/logman" | ||
|
||
"github.com/opentdp/wechat-rest/wcferry" | ||
) | ||
|
||
var socketReceiverKey = "" | ||
var socketReceiverList = map[*websocket.Conn]*WebSocketMutex{} | ||
|
||
type WebSocketMutex struct { | ||
*websocket.Conn | ||
mu sync.Mutex | ||
} | ||
|
||
func (w *WebSocketMutex) WriteJSON(v any) error { | ||
w.mu.Lock() | ||
defer w.mu.Unlock() | ||
return w.Conn.WriteJSON(v) | ||
} | ||
|
||
func (wc *Controller) enableSocketReceiver(ws *websocket.Conn) error { | ||
|
||
logman.Warn("enable receiver", "socket", ws.RemoteAddr().String()) | ||
|
||
if socketReceiverList[ws] != nil { | ||
return errors.New("socket already exists") | ||
} | ||
|
||
wm := &WebSocketMutex{ws, sync.Mutex{}} | ||
|
||
if len(socketReceiverList) == 0 { | ||
key, err := wc.EnrollReceiver(true, func(msg *wcferry.WxMsg) { | ||
ret := wcferry.ParseWxMsg(msg) | ||
for s := range socketReceiverList { | ||
logman.Info("call receiver", "addr", s.RemoteAddr(), "Id", ret.Id) | ||
go s.WriteJSON(ret) | ||
} | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
socketReceiverKey = key | ||
} | ||
|
||
socketReceiverList[ws] = wm | ||
return nil | ||
|
||
} | ||
|
||
func (wc *Controller) disableSocketReceiver(ws *websocket.Conn) error { | ||
|
||
logman.Warn("disable receiver", "addr", ws.RemoteAddr()) | ||
|
||
if socketReceiverList[ws] == nil { | ||
return errors.New("socket not exists") | ||
} | ||
|
||
delete(socketReceiverList, ws) | ||
if len(socketReceiverList) == 0 { | ||
return wc.DisableReceiver(socketReceiverKey) | ||
} | ||
|
||
return nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters