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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"io"
|
|
||||||
jsoniter "github.com/json-iterator/go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func removeSocket(s string) {
|
func removeSocket(s string) {
|
||||||
|
|
@ -45,6 +45,6 @@ func socketHandler(conn net.Conn) {
|
||||||
fmt.Println("json error:", err.Error())
|
fmt.Println("json error:", err.Error())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
ws.Send <- wsMessage{Message: raw, Decode: true}
|
ws.Send <- raw
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,19 +14,15 @@ type enterChanel struct {
|
||||||
Chanel string
|
Chanel string
|
||||||
}
|
}
|
||||||
|
|
||||||
type wsMessage struct {
|
|
||||||
Message []byte
|
|
||||||
Decode bool
|
|
||||||
}
|
|
||||||
|
|
||||||
var upgrader = websocket.Upgrader{
|
var upgrader = websocket.Upgrader{
|
||||||
ReadBufferSize: 1024,
|
ReadBufferSize: 1024,
|
||||||
WriteBufferSize: 1024,
|
WriteBufferSize: 1024,
|
||||||
CheckOrigin: func(r *http.Request) bool {
|
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 true
|
||||||
}
|
//}
|
||||||
return false
|
//return false
|
||||||
|
return true
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,17 +32,15 @@ var (
|
||||||
json = jsoniter.ConfigCompatibleWithStandardLibrary
|
json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||||
|
|
||||||
ws = struct {
|
ws = struct {
|
||||||
//Count chan int
|
Send chan []byte
|
||||||
|
Enter chan enterChanel
|
||||||
Add chan *websocket.Conn
|
Add chan *websocket.Conn
|
||||||
Del 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),
|
Add: make(chan *websocket.Conn, 100),
|
||||||
Del: make(chan *websocket.Conn, 100),
|
Del: make(chan *websocket.Conn, 100),
|
||||||
Send: make(chan wsMessage, 100),
|
|
||||||
Enter: make(chan enterChanel, 100),
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -60,41 +54,28 @@ func broadcast() {
|
||||||
case conn := <-ws.Del:
|
case conn := <-ws.Del:
|
||||||
delete(wsClients, conn)
|
delete(wsClients, conn)
|
||||||
|
|
||||||
//case <-ws.Count:
|
|
||||||
// ws.Count <- len(wsClients)
|
|
||||||
|
|
||||||
case r := <-ws.Enter:
|
case r := <-ws.Enter:
|
||||||
wsClients[r.Conn] = r.Chanel
|
wsClients[r.Conn] = r.Chanel
|
||||||
|
|
||||||
case r := <-ws.Send:
|
case r := <-ws.Send:
|
||||||
sendMessage(r.Message, r.Decode)
|
sendMessage(r)
|
||||||
|
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
fmt.Println("WebSocket:", len(wsClients))
|
fmt.Println("WebSocket:", len(wsClients))
|
||||||
sendMessage([]byte("ping"), false)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendMessage(message []byte, decode bool) {
|
func sendMessage(message []byte) {
|
||||||
chanel := ""
|
input := struct {
|
||||||
if decode {
|
Chanel string `json:"chanel"`
|
||||||
input := struct {
|
}{}
|
||||||
Chanel string `json:"chanel"`
|
if err := json.Unmarshal(message, &input); err != nil {
|
||||||
}{}
|
fmt.Println("json error: ", err.Error())
|
||||||
if err := json.Unmarshal(message, &input); err != nil {
|
return
|
||||||
fmt.Println("json error: ", err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if input.Chanel != "" {
|
|
||||||
chanel = input.Chanel
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//fmt.Println(string(message), decode, chanel)
|
|
||||||
|
|
||||||
for conn, ch := range wsClients {
|
for conn, ch := range wsClients {
|
||||||
if decode && ch != chanel {
|
if ch != input.Chanel {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := conn.WriteMessage(1, message); err != nil {
|
if err := conn.WriteMessage(1, message); err != nil {
|
||||||
|
|
@ -125,8 +106,9 @@ func readWS(conn *websocket.Conn) {
|
||||||
"chaturbate": true,
|
"chaturbate": true,
|
||||||
"bongacams": true,
|
"bongacams": true,
|
||||||
"stripchat": true,
|
"stripchat": true,
|
||||||
|
"camsoda": true,
|
||||||
}
|
}
|
||||||
|
ping := time.Now().Unix()
|
||||||
for {
|
for {
|
||||||
_, message, err := conn.ReadMessage()
|
_, message, err := conn.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -134,10 +116,28 @@ func readWS(conn *websocket.Conn) {
|
||||||
return
|
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] {
|
input := struct {
|
||||||
ws.Enter <- enterChanel{Conn: conn, Chanel: input}
|
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