8000 fix: 同時にブロードキャストしないようにする by ikafly144 · Pull Request #53 · sabafly/gobot · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: 同時にブロードキャストしないようにする #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
< 8000 div class="js-unified-diff-view">
Diff view
12 changes: 9 additions & 3 deletions api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"encoding/json"
"net/http"
"sync"

"github.com/gorilla/websocket"
"github.com/sabafly/gobot/pkg/lib/logging"
Expand All @@ -39,8 +40,9 @@ type Event struct {

// ウェブソケットをハンドルする
type WebsocketHandler struct {
Conn []*websocket.Conn
Seq int64
WSMutex sync.Mutex
Conn []*websocket.Conn
Seq int64
}

// 新たなウェブソケットハンダラを生成する
Expand Down Expand Up @@ -107,6 +109,10 @@ func (h *WebsocketHandler) handlerLoop(ws *websocket.Conn) {
// TODO: ウェブソケット接続がクローズしたときに破綻する
func (h *WebsocketHandler) Broadcast(f func(*websocket.Conn)) {
for _, c := range h.Conn {
go f(c)
go func(c *websocket.Conn) {
h.WSMutex.Lock()
f(c)
h.WSMutex.Unlock()
}(c)
}
}
0