mirror of
https://github.com/nxshock/gron.git
synced 2025-04-20 01:31:50 +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 {
|
type WsConnections struct {
|
||||||
connections map[*WsConnection]struct{}
|
connections map[*websocket.Conn]struct{}
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,24 +24,19 @@ func (wc *WsConnections) Add(c *websocket.Conn) {
|
|||||||
wc.mutex.Lock()
|
wc.mutex.Lock()
|
||||||
defer wc.mutex.Unlock()
|
defer wc.mutex.Unlock()
|
||||||
|
|
||||||
wc.connections[NewWsConnection(c)] = struct{}{}
|
wc.connections[c] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wc *WsConnections) Delete(c *websocket.Conn) {
|
func (wc *WsConnections) Delete(c *websocket.Conn) {
|
||||||
wc.mutex.Lock()
|
wc.mutex.Lock()
|
||||||
defer wc.mutex.Unlock()
|
defer wc.mutex.Unlock()
|
||||||
|
|
||||||
for k := range wc.connections {
|
delete(wc.connections, c)
|
||||||
if k.w == c {
|
|
||||||
delete(wc.connections, k)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wc *WsConnections) Send(message interface{}) {
|
func (wc *WsConnections) Send(message interface{}) {
|
||||||
for conn := range wc.connections {
|
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{
|
var wsConnections = &WsConnections{
|
||||||
connections: make(map[*WsConnection]struct{})}
|
connections: make(map[*websocket.Conn]struct{})}
|
||||||
|
|
||||||
func httpServer(listenAddress string) {
|
func httpServer(listenAddress string) {
|
||||||
if listenAddress == "none" {
|
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…
x
Reference in New Issue
Block a user