代码拉取完成,页面将自动刷新
同步操作将从 xgc/douyin-monitor 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package main
import (
"fmt"
"github.com/gorilla/websocket"
websocket2 "io.github.javpower/douyin-monitor/websocket"
"log"
"net/http"
"sync"
"sync/atomic"
)
type RRoom struct {
address string
callback string
connected int32
wsConnect *websocket.Conn
}
func NewRRoom(address string, callback string) *RRoom {
return &RRoom{
address: address,
callback: callback,
connected: 0,
}
}
func (r *RRoom) Connect() {
rr, err := NewRoom(r.address, r.callback)
if err != nil {
log.Println(err)
return
}
err = rr.Connect()
if err != nil {
log.Println(err)
return
}
r.wsConnect = rr.wsConnect
atomic.StoreInt32(&r.connected, 1)
rooms[r.address] = r
}
func (r *RRoom) Close() {
if atomic.CompareAndSwapInt32(&r.connected, 1, 0) {
r.wsConnect.Close()
}
}
var rooms = make(map[string]*RRoom)
var mu sync.Mutex
func handleAPI(w http.ResponseWriter, req *http.Request) {
address := req.FormValue("roomId")
command := req.FormValue("command")
callback := req.FormValue("callback")
if address == "" || command == "" {
http.Error(w, "Address and command are required parameters", http.StatusBadRequest)
return
}
mu.Lock()
defer mu.Unlock()
if room, ok := rooms[address]; ok {
if command == "connect" {
return
} else if command == "close" {
room.Close()
delete(rooms, address)
}
} else if command == "connect" {
room = NewRRoom(address, callback)
go room.Connect() // 使用goroutine在后台连接
}
fmt.Fprintln(w, "OK")
}
var Wss *websocket2.WebSocketServer
func main() {
http.HandleFunc("/api", handleAPI)
Wss = websocket2.NewWebSocketServer()
http.HandleFunc("/ws", Wss.HandleWebSocket)
log.Println("Starting server on http://localhost:8709")
log.Fatal(http.ListenAndServe(":8709", nil))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。