forked from cfadmin-cn/cfadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws.lua
More file actions
53 lines (47 loc) · 1.47 KB
/
ws.lua
File metadata and controls
53 lines (47 loc) · 1.47 KB
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
local class = require "class"
local cf = require "cf"
local json = require "json"
local MQ = require "MQ"
local websocket = class("websocket")
function websocket:ctor(opt)
self.ws = opt.ws -- websocket对象
self.send_masked = false -- 掩码(默认为false, 不建议修改或者使用)
self.max_payload_len = 65535 -- 最大有效载荷长度(默认为65535, 不建议修改或者使用)
self.timeout = 15
self.count = 0
self.mq = MQ:new({host = 'localhost', port = 6379, type = 'redis'})
end
function websocket:on_open()
print('on_open')
self.timer = cf.at(0.01, function ( ... ) -- 定时器
self.count = self.count + 1
self.ws:send(tostring(self.count))
end)
self.mq:on('/test/*', function (msg) -- 消息队列
if not msg then
self.ws:send('{"code":500,"message":"无法连接到mq(reds)"}')
return self.ws:close()
end
self.ws:send(json.encode(msg))
end)
end
function websocket:on_message(data, typ)
print('on_message', self.ws, data)
self.ws:send('welcome')
self.ws:close(data)
end
function websocket:on_error(error)
print('on_error', self.ws, error)
end
function websocket:on_close(data)
print('on_close', self.ws, data)
if self.mq then -- 清理消息队列
self.mq:close()
self.mq = nil
end
if self.timer then -- 清理定时器
self.timer:stop()
self.timer = nil
end
end
return websocket