1local context = G.botContext
2
3context.BotServer = {}
4context.BotServer.url = "ws://bot.otclient.ovh:8000/"
5context.BotServer.timeout = 3
6context.BotServer.ping = 0
7context.BotServer._callbacks = {}
8context.BotServer._lastMessageId = 0
9context.BotServer._wasConnected = true -- show first warning
10
11context.BotServer.init = function(name, channel)
12 if not channel or not name or channel:len() < 1 or name:len() < 1 then
13 return context.error("Invalid params for BotServer.init")
14 end
15 if context.BotServer._websocket then
16 return context.error("BotServer is already initialized")
17 end
18 context.BotServer._websocket = HTTP.WebSocketJSON(context.BotServer.url, {
19 onMessage = function(message, socketId)
20 if not context._websockets[socketId] then
21 return g_http.cancel(socketId)
22 end
23 if not context.BotServer._websocket or context.BotServer._websocket.id ~= socketId then
24 return g_http.cancel(socketId)
25 end
26 context.BotServer._wasConnected = true
27 if message["type"] == "ping" then
28 context.BotServer.ping = message["ping"]
29 return context.BotServer._websocket.send({type="ping"})
30 end
31 if message["type"] == "message" then
32 context.BotServer._lastMessageId = message["id"]
33 local topics = context.BotServer._callbacks[message["topic"]]
34 if topics then
35 for i=1,#topics do
36 topics[i](message["name"], message["message"], message["topic"])
37 end
38 end
39 topics = context.BotServer._callbacks["*"]
40 if topics then
41 for i=1,#topics do
42 topics[i](message["name"], message["message"], message["topic"])
43 end
44 end
45 return
46 end
47 end,
48 onClose = function(message, socketId)
49 if not context._websockets[socketId] then
50 return
51 end
52 context._websockets[socketId] = nil
53 if not context.BotServer._websocket or context.BotServer._websocket.id ~= socketId then
54 return
55 end
56 if context.BotServer._wasConnected then
57 context.warn("BotServer disconnected")
58 end
59 context.BotServer._wasConnected = false
60 context.BotServer._websocket = nil
61 context.BotServer.ping = 0
62 context.BotServer.init(name, channel)
63 end
64 }, context.BotServer.timeout)
65 context._websockets[context.BotServer._websocket.id] = 1
66 context.BotServer._websocket.send({type="init", name=name, channel=channel, lastMessage=context.BotServer._lastMessageId})
67end
68
69context.BotServer.terminate = function()
70 if context.BotServer._websocket then
71 context.BotServer._websocket:close()
72 context.BotServer._websocket = nil
73 end
74end
75
76context.BotServer.listen = function(topic, callback) -- callback = function(name, message, topic) -- message is parsed json = table
77 if not context.BotServer._websocket then
78 return context.error("BotServer is not initialized")
79 end
80 if not context.BotServer._callbacks[topic] then
81 context.BotServer._callbacks[topic] = {}
82 end
83 table.insert(context.BotServer._callbacks[topic], callback)
84end
85
86context.BotServer.send = function(topic, message)
87 if not context.BotServer._websocket then
88 return context.error("BotServer is not initialized")
89 end
90 context.BotServer._websocket.send({type="message", topic=topic, message=message})
91end
92