mirror of
https://github.com/statbate/supreme-disco.git
synced 2026-08-11 03:22:41 +00:00
update
This commit is contained in:
parent
dd7ac94739
commit
e0d1877fb9
1 changed files with 20 additions and 47 deletions
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
|
@ -14,11 +15,6 @@ type Client struct {
|
||||||
Conn *websocket.Conn
|
Conn *websocket.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
type sendMsg struct {
|
|
||||||
Message []byte
|
|
||||||
Conn *websocket.Conn
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
wsClients = make(map[*Client]bool)
|
wsClients = make(map[*Client]bool)
|
||||||
|
|
||||||
|
|
@ -26,12 +22,10 @@ var (
|
||||||
|
|
||||||
ws = struct {
|
ws = struct {
|
||||||
Broadcast chan []byte
|
Broadcast chan []byte
|
||||||
Send chan sendMsg
|
|
||||||
Add chan *Client
|
Add chan *Client
|
||||||
Del chan *Client
|
Del chan *Client
|
||||||
}{
|
}{
|
||||||
Broadcast: make(chan []byte, 100),
|
Broadcast: make(chan []byte, 100),
|
||||||
Send: make(chan sendMsg, 100),
|
|
||||||
Add: make(chan *Client, 100),
|
Add: make(chan *Client, 100),
|
||||||
Del: make(chan *Client, 100),
|
Del: make(chan *Client, 100),
|
||||||
}
|
}
|
||||||
|
|
@ -59,24 +53,15 @@ func broadcast() {
|
||||||
case conn := <-ws.Del:
|
case conn := <-ws.Del:
|
||||||
delete(wsClients, conn)
|
delete(wsClients, conn)
|
||||||
|
|
||||||
case r := <-ws.Send:
|
|
||||||
sendMessage(r.Conn, r.Message)
|
|
||||||
|
|
||||||
case r := <-ws.Broadcast:
|
case r := <-ws.Broadcast:
|
||||||
sendBroadcast(r)
|
sendBroadcast(r)
|
||||||
|
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
fmt.Println("WebSocket:", len(wsClients))
|
fmt.Println("WebSocket:", len(wsClients), "Goroutines:", runtime.NumGoroutine())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendMessage(conn *websocket.Conn, message []byte) {
|
|
||||||
if err := conn.WriteMessage(1, message); err != nil {
|
|
||||||
conn.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func sendBroadcast(message []byte) {
|
func sendBroadcast(message []byte) {
|
||||||
input := struct {
|
input := struct {
|
||||||
Chanel string `json:"chanel"`
|
Chanel string `json:"chanel"`
|
||||||
|
|
@ -89,23 +74,10 @@ func sendBroadcast(message []byte) {
|
||||||
if client.Chanel != input.Chanel {
|
if client.Chanel != input.Chanel {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
sendMessage(client.Conn, message)
|
if err := client.Conn.WriteMessage(1, message); err != nil {
|
||||||
|
client.Conn.Close()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func enterChannel(conn *websocket.Conn, chanel string) (*Client, bool) {
|
|
||||||
chanels := map[string]bool{
|
|
||||||
"chaturbate": true,
|
|
||||||
"bongacams": true,
|
|
||||||
"stripchat": true,
|
|
||||||
"camsoda": true,
|
|
||||||
}
|
}
|
||||||
client := &Client{Conn: conn, Chanel: chanel}
|
|
||||||
if chanels[chanel] {
|
|
||||||
ws.Add <- client
|
|
||||||
return client, true
|
|
||||||
}
|
|
||||||
return client, false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func wsHandler(w http.ResponseWriter, r *http.Request) {
|
func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
@ -133,29 +105,30 @@ func readWS(conn *websocket.Conn) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client, ok := enterChannel(conn, input.Chanel)
|
client := &Client{Conn: conn, Chanel: input.Chanel}
|
||||||
if !ok {
|
|
||||||
|
chanels := map[string]bool{
|
||||||
|
"chaturbate": true,
|
||||||
|
"bongacams": true,
|
||||||
|
"stripchat": true,
|
||||||
|
"camsoda": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
if !chanels[input.Chanel] {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ws.Add <- client
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
ws.Del <- client
|
ws.Del <- client
|
||||||
}()
|
}()
|
||||||
|
|
||||||
ping := time.Now().Unix()
|
|
||||||
for {
|
for {
|
||||||
conn.SetReadDeadline(time.Now().Add(30 * time.Minute))
|
conn.SetReadDeadline(time.Now().Add(30 * time.Minute))
|
||||||
_, message, err := conn.ReadMessage()
|
_, _, err := conn.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if string(message) == "ping" {
|
|
||||||
if time.Now().Unix() > ping {
|
|
||||||
ws.Send <- sendMsg{Conn: conn, Message: []byte("pong")}
|
|
||||||
ping = time.Now().Unix() + 15
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue