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
2ab97d2d10
commit
4a56900df4
2 changed files with 44 additions and 44 deletions
|
|
@ -2,11 +2,11 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"io"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func removeSocket(s string) {
|
||||
|
|
@ -45,6 +45,6 @@ func socketHandler(conn net.Conn) {
|
|||
fmt.Println("json error:", err.Error())
|
||||
continue
|
||||
}
|
||||
ws.Send <- wsMessage{Message: raw, Decode: true}
|
||||
ws.Send <- raw
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,19 +14,15 @@ type enterChanel struct {
|
|||
Chanel string
|
||||
}
|
||||
|
||||
type wsMessage struct {
|
||||
Message []byte
|
||||
Decode bool
|
||||
}
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
if r.Header.Get("origin") == "https://statbate.com" {
|
||||
//if r.Header.Get("origin") == "https://statbate.com" {
|
||||
// return true
|
||||
//}
|
||||
//return false
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
}
|
||||
|
||||
|
|
@ -36,17 +32,15 @@ var (
|
|||
json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
ws = struct {
|
||||
//Count chan int
|
||||
Send chan []byte
|
||||
Enter chan enterChanel
|
||||
Add chan *websocket.Conn
|
||||
Del chan *websocket.Conn
|
||||
Send chan wsMessage
|
||||
Enter chan enterChanel
|
||||
}{
|
||||
//Count: make(chan int, 100),
|
||||
Send: make(chan []byte, 100),
|
||||
Enter: make(chan enterChanel, 100),
|
||||
Add: make(chan *websocket.Conn, 100),
|
||||
Del: make(chan *websocket.Conn, 100),
|
||||
Send: make(chan wsMessage, 100),
|
||||
Enter: make(chan enterChanel, 100),
|
||||
}
|
||||
)
|
||||
|
||||
|
|
@ -60,25 +54,19 @@ func broadcast() {
|
|||
case conn := <-ws.Del:
|
||||
delete(wsClients, conn)
|
||||
|
||||
//case <-ws.Count:
|
||||
// ws.Count <- len(wsClients)
|
||||
|
||||
case r := <-ws.Enter:
|
||||
wsClients[r.Conn] = r.Chanel
|
||||
|
||||
case r := <-ws.Send:
|
||||
sendMessage(r.Message, r.Decode)
|
||||
sendMessage(r)
|
||||
|
||||
case <-ticker.C:
|
||||
fmt.Println("WebSocket:", len(wsClients))
|
||||
sendMessage([]byte("ping"), false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func sendMessage(message []byte, decode bool) {
|
||||
chanel := ""
|
||||
if decode {
|
||||
func sendMessage(message []byte) {
|
||||
input := struct {
|
||||
Chanel string `json:"chanel"`
|
||||
}{}
|
||||
|
|
@ -86,15 +74,8 @@ func sendMessage(message []byte, decode bool) {
|
|||
fmt.Println("json error: ", err.Error())
|
||||
return
|
||||
}
|
||||
if input.Chanel != "" {
|
||||
chanel = input.Chanel
|
||||
}
|
||||
}
|
||||
|
||||
//fmt.Println(string(message), decode, chanel)
|
||||
|
||||
for conn, ch := range wsClients {
|
||||
if decode && ch != chanel {
|
||||
if ch != input.Chanel {
|
||||
continue
|
||||
}
|
||||
if err := conn.WriteMessage(1, message); err != nil {
|
||||
|
|
@ -125,8 +106,9 @@ func readWS(conn *websocket.Conn) {
|
|||
"chaturbate": true,
|
||||
"bongacams": true,
|
||||
"stripchat": true,
|
||||
"camsoda": true,
|
||||
}
|
||||
|
||||
ping := time.Now().Unix()
|
||||
for {
|
||||
_, message, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
|
|
@ -134,10 +116,28 @@ func readWS(conn *websocket.Conn) {
|
|||
return
|
||||
}
|
||||
|
||||
input := string(message)
|
||||
if string(message) == "ping" {
|
||||
if time.Now().Unix() > ping {
|
||||
if err := conn.WriteMessage(1, []byte("pong")); err != nil {
|
||||
fmt.Println("write message", err.Error())
|
||||
return
|
||||
}
|
||||
ping = time.Now().Unix() + 15
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if len(input) < 32 && chanels[input] {
|
||||
ws.Enter <- enterChanel{Conn: conn, Chanel: input}
|
||||
input := struct {
|
||||
Chanel string `json:"chanel"`
|
||||
}{}
|
||||
|
||||
if err := json.Unmarshal(message, &input); err != nil {
|
||||
fmt.Println("wrong json", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if chanels[input.Chanel] {
|
||||
ws.Enter <- enterChanel{Conn: conn, Chanel: input.Chanel}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue