1 Star 0 Fork 8

dahezi999/douyin-monitor

forked from xgc/douyin-monitor 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.go 1.73 KB
一键复制 编辑 原始数据 按行查看 历史
xgc 提交于 2024-04-25 17:06 . first commit
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))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/dahezi/douyin-monitor.git
git@gitee.com:dahezi/douyin-monitor.git
dahezi
douyin-monitor
douyin-monitor
main

搜索帮助