Skip to content

Commit 8736e62

Browse files
author
Zing
committed
refactor(websocket): 移除构造函数并在注册时初始化连接
将WebSocket模块的初始化逻辑从构造函数移动到Register方法中
1 parent 1f302ff commit 8736e62

2 files changed

Lines changed: 14 additions & 16 deletions

File tree

js_engine/model/autogo/websocket/websocket_inject.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ type WebSocketModule struct {
1616
mu sync.Mutex
1717
}
1818

19-
// New 创建新的 WebSocket 模块实例
20-
func New() *WebSocketModule {
21-
return &WebSocketModule{
22-
connections: make(map[int]*websocket.Conn),
23-
nextHandle: 1,
24-
}
25-
}
26-
2719
// Name 返回模块名称
2820
func (m *WebSocketModule) Name() string {
2921
return "websocket"
@@ -38,6 +30,13 @@ func (m *WebSocketModule) IsAvailable() bool {
3830
func (m *WebSocketModule) Register(engine model.Engine) error {
3931
vm := engine.GetVM()
4032

33+
m.mu.Lock()
34+
if m.connections == nil {
35+
m.connections = make(map[int]*websocket.Conn)
36+
m.nextHandle = 1
37+
}
38+
m.mu.Unlock()
39+
4140
wsObj := vm.NewObject()
4241
vm.Set("websocket", wsObj)
4342

lua_engine/model/autogo/websocket/websocket_inject.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,6 @@ type WebSocketModule struct {
1616
mu sync.Mutex
1717
}
1818

19-
// New 创建新的 WebSocket 模块实例
20-
func New() *WebSocketModule {
21-
return &WebSocketModule{
22-
connections: make(map[int]*websocket.Conn),
23-
nextHandle: 1,
24-
}
25-
}
26-
2719
// Name 返回模块名称
2820
func (m *WebSocketModule) Name() string {
2921
return "websocket"
@@ -38,6 +30,13 @@ func (m *WebSocketModule) IsAvailable() bool {
3830
func (m *WebSocketModule) Register(engine model.Engine) error {
3931
state := engine.GetState()
4032

33+
m.mu.Lock()
34+
if m.connections == nil {
35+
m.connections = make(map[int]*websocket.Conn)
36+
m.nextHandle = 1
37+
}
38+
m.mu.Unlock()
39+
4140
wsObj := state.NewTable()
4241
state.SetGlobal("websocket", wsObj)
4342

0 commit comments

Comments
 (0)