mirror of
https://github.com/nxshock/gron.git
synced 2024-11-27 03:41:00 +05:00
Compare commits
No commits in common. "b6e32d3b9821bf3302d52e11edd652c428587d51" and "252141ae01108f780db0a6d7c63497d36119a92a" have entirely different histories.
b6e32d3b98
...
252141ae01
@ -16,7 +16,7 @@ import (
|
||||
)
|
||||
|
||||
type WsConnections struct {
|
||||
connections map[*WsConnection]struct{}
|
||||
connections map[*websocket.Conn]struct{}
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
@ -24,24 +24,19 @@ func (wc *WsConnections) Add(c *websocket.Conn) {
|
||||
wc.mutex.Lock()
|
||||
defer wc.mutex.Unlock()
|
||||
|
||||
wc.connections[NewWsConnection(c)] = struct{}{}
|
||||
wc.connections[c] = struct{}{}
|
||||
}
|
||||
|
||||
func (wc *WsConnections) Delete(c *websocket.Conn) {
|
||||
wc.mutex.Lock()
|
||||
defer wc.mutex.Unlock()
|
||||
|
||||
for k := range wc.connections {
|
||||
if k.w == c {
|
||||
delete(wc.connections, k)
|
||||
break
|
||||
}
|
||||
}
|
||||
delete(wc.connections, c)
|
||||
}
|
||||
|
||||
func (wc *WsConnections) Send(message interface{}) {
|
||||
for conn := range wc.connections {
|
||||
go func(conn *WsConnection) { _ = conn.Send(message) }(conn)
|
||||
go func(conn *websocket.Conn) { _ = conn.WriteJSON(message) }(conn)
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,7 +49,7 @@ var upgrader = websocket.Upgrader{
|
||||
}
|
||||
|
||||
var wsConnections = &WsConnections{
|
||||
connections: make(map[*WsConnection]struct{})}
|
||||
connections: make(map[*websocket.Conn]struct{})}
|
||||
|
||||
func httpServer(listenAddress string) {
|
||||
if listenAddress == "none" {
|
||||
|
2
make.bat
2
make.bat
@ -1 +1 @@
|
||||
go build -ldflags "-s -w" -buildmode=pie -trimpath
|
||||
go build -ldflags "-s -w -H windowsgui" -buildmode=pie -trimpath
|
||||
|
23
wconn.go
23
wconn.go
@ -1,23 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type WsConnection struct {
|
||||
w *websocket.Conn
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewWsConnection(w *websocket.Conn) *WsConnection {
|
||||
return &WsConnection{w: w}
|
||||
}
|
||||
|
||||
func (w *WsConnection) Send(message any) error {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
|
||||
return w.w.WriteJSON(message)
|
||||
}
|
Loading…
Reference in New Issue
Block a user