-
Notifications
You must be signed in to change notification settings - Fork 0
/
gluawebsocket.go
70 lines (58 loc) · 1.39 KB
/
gluawebsocket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package gluawebsocket
import (
"net/http"
"github.com/gorilla/websocket"
lua "github.com/yuin/gopher-lua"
)
func Preload(L *lua.LState) {
L.PreloadModule("websocket", Loader)
}
var exports = map[string]lua.LGFunction{
"dial": dial,
"get_msg_type": getMsgType,
}
func Loader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), exports)
L.Push(mod)
registerWebsocketConnType(L)
return 1
}
func getMsgType(L *lua.LState) int {
msgTypeString := L.CheckString(1)
switch msgTypeString {
case "text":
L.Push(lua.LNumber(websocket.TextMessage))
case "binary":
L.Push(lua.LNumber(websocket.BinaryMessage))
case "ping":
L.Push(lua.LNumber(websocket.PingMessage))
case "pong":
L.Push(lua.LNumber(websocket.PongMessage))
case "close":
L.Push(lua.LNumber(websocket.CloseMessage))
default:
L.Push(lua.LNumber(-1))
}
return 1
}
func dial(L *lua.LState) int {
addr := L.CheckString(1)
headerInput := L.CheckTable(2)
header := make(http.Header)
headerInput.ForEach(func(key lua.LValue, value lua.LValue) {
header.Set(key.String(), value.String())
})
wsConn, _, err := websocket.DefaultDialer.Dial(addr, header)
ud := L.NewUserData()
ud.Value = wsConn
L.SetMetatable(ud, L.GetTypeMetatable(WEBSOCKET_CONN_TYPENAME))
L.Push(ud)
// TODO support http response
L.Push(lua.LNil)
if err != nil {
L.Push(lua.LString(err.Error()))
} else {
L.Push(lua.LNil)
}
return 3
}