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
2299b71073
commit
dd7ac94739
1 changed files with 66 additions and 60 deletions
|
|
@ -9,45 +9,43 @@ import (
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
type enterChanel struct {
|
type Client struct {
|
||||||
Conn *websocket.Conn
|
|
||||||
Chanel string
|
Chanel string
|
||||||
|
Conn *websocket.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
type sendMsg struct {
|
type sendMsg struct {
|
||||||
Conn *websocket.Conn
|
|
||||||
Message []byte
|
Message []byte
|
||||||
}
|
Conn *websocket.Conn
|
||||||
|
|
||||||
var upgrader = websocket.Upgrader{
|
|
||||||
ReadBufferSize: 1024,
|
|
||||||
WriteBufferSize: 1024,
|
|
||||||
CheckOrigin: func(r *http.Request) bool {
|
|
||||||
//if r.Header.Get("origin") == "https://statbate.com" {
|
|
||||||
// return true
|
|
||||||
//}
|
|
||||||
//return false
|
|
||||||
return true
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
wsClients = make(map[*websocket.Conn]string)
|
wsClients = make(map[*Client]bool)
|
||||||
|
|
||||||
json = jsoniter.ConfigCompatibleWithStandardLibrary
|
json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||||
|
|
||||||
ws = struct {
|
ws = struct {
|
||||||
Broadcast chan []byte
|
Broadcast chan []byte
|
||||||
Send chan sendMsg
|
Send chan sendMsg
|
||||||
Enter chan enterChanel
|
Add chan *Client
|
||||||
Add chan *websocket.Conn
|
Del chan *Client
|
||||||
Del chan *websocket.Conn
|
|
||||||
}{
|
}{
|
||||||
Broadcast: make(chan []byte, 100),
|
Broadcast: make(chan []byte, 100),
|
||||||
Send: make(chan sendMsg, 100),
|
Send: make(chan sendMsg, 100),
|
||||||
Enter: make(chan enterChanel, 100),
|
Add: make(chan *Client, 100),
|
||||||
Add: make(chan *websocket.Conn, 100),
|
Del: make(chan *Client, 100),
|
||||||
Del: make(chan *websocket.Conn, 100),
|
}
|
||||||
|
|
||||||
|
upgrader = websocket.Upgrader{
|
||||||
|
ReadBufferSize: 1024,
|
||||||
|
WriteBufferSize: 1024,
|
||||||
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
|
params := r.URL.Query()
|
||||||
|
if params["api_key"] != nil || r.Header.Get("origin") == "https://statbate.com" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -55,16 +53,11 @@ func broadcast() {
|
||||||
ticker := time.NewTicker(30 * time.Second)
|
ticker := time.NewTicker(30 * time.Second)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case conn := <-ws.Add:
|
case client := <-ws.Add:
|
||||||
wsClients[conn] = ""
|
wsClients[client] = true
|
||||||
//fmt.Println("add conn", len(wsClients))
|
|
||||||
|
|
||||||
case conn := <-ws.Del:
|
case conn := <-ws.Del:
|
||||||
delete(wsClients, conn)
|
delete(wsClients, conn)
|
||||||
//fmt.Println("delete conn", len(wsClients))
|
|
||||||
|
|
||||||
case r := <-ws.Enter:
|
|
||||||
wsClients[r.Conn] = r.Chanel
|
|
||||||
|
|
||||||
case r := <-ws.Send:
|
case r := <-ws.Send:
|
||||||
sendMessage(r.Conn, r.Message)
|
sendMessage(r.Conn, r.Message)
|
||||||
|
|
@ -92,14 +85,29 @@ func sendBroadcast(message []byte) {
|
||||||
fmt.Println("json error: ", err.Error())
|
fmt.Println("json error: ", err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for conn, ch := range wsClients {
|
for client := range wsClients {
|
||||||
if ch != input.Chanel {
|
if client.Chanel != input.Chanel {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
sendMessage(conn, message)
|
sendMessage(client.Conn, message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -111,23 +119,34 @@ func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
func readWS(conn *websocket.Conn) {
|
func readWS(conn *websocket.Conn) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
ws.Add <- conn
|
conn.SetReadDeadline(time.Now().Add(30 * time.Second))
|
||||||
|
_, message, err := conn.ReadMessage()
|
||||||
defer func() {
|
if err != nil {
|
||||||
ws.Del <- conn
|
return
|
||||||
}()
|
}
|
||||||
|
|
||||||
chanels := map[string]bool{
|
input := struct {
|
||||||
"chaturbate": true,
|
Chanel string `json:"chanel"`
|
||||||
"bongacams": true,
|
}{}
|
||||||
"stripchat": true,
|
|
||||||
"camsoda": true,
|
if err := json.Unmarshal(message, &input); err != nil {
|
||||||
}
|
return
|
||||||
ping := time.Now().Unix()
|
}
|
||||||
for {
|
|
||||||
|
client, ok := enterChannel(conn, input.Chanel)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
ws.Del <- client
|
||||||
|
}()
|
||||||
|
|
||||||
|
ping := time.Now().Unix()
|
||||||
|
for {
|
||||||
|
conn.SetReadDeadline(time.Now().Add(30 * time.Minute))
|
||||||
_, message, err := conn.ReadMessage()
|
_, message, err := conn.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//fmt.Println("readWS", err.Error())
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,18 +157,5 @@ func readWS(conn *websocket.Conn) {
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
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