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
41 lines (35 loc) · 1.12 KB
/
ws.lua
File metadata and controls
41 lines (35 loc) · 1.12 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
local class = require "class"
local cf = require "cf"
local json = require "json"
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 -- 默认为一直等待, 非number类型会导致异常.
self.count = 0
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)
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.timer then -- 清理定时器
print("清理定时器")
self.timer:stop()
self.timer = nil
end
end
return websocket