-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest.lua
More file actions
58 lines (53 loc) · 1.69 KB
/
test.lua
File metadata and controls
58 lines (53 loc) · 1.69 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
54
55
56
57
58
package.preload["socket"] = function()end
local ws = require"websocket"
local client = {
socket = {},
_buffer = "",
_length = 2,
_head = nil,
}
local res, head, err
local function receive(t)
return function(_, n)
if #t>0 then
local ret = t[1]
if n<#ret then
ret, t[1] = ret:sub(1,n), ret:sub(n+1)
else
table.remove(t, 1)
end
return ret, nil, nil
else
return nil, "timeout", nil
end
end
end
--空消息
client.socket.receive = receive{"\x81", "\x00"}
res, head, err = ws.read(client)
assert(res==nil and head==nil and err=="buffer length less than 2")
res, head, err = ws.read(client)
assert(res=="" and head==0x81 and err==nil)
--1字节消息
client.socket.receive = receive{"\x81\x01"}
res, head, err = ws.read(client)
assert(res==nil and head==nil and err==nil)
client.socket.receive = receive{"\x31"}
res, head, err = ws.read(client)
assert(res=="1" and head==0x81 and err==nil)
--5字节消息
client.socket.receive = receive{"\x81\x05", "12", "345"}
res, head, err = ws.read(client)
assert(res==nil and head==nil and err=="buffer length less than 5")
res, head, err = ws.read(client)
assert(res=="12345" and head==0x81 and err==nil)
--200字节消息
local s = "" for i=1,100 do s=s..i%5 end
client.socket.receive = receive{"\x81\x7e", "\x00", "\xc8", s, s}
res, head, err = ws.read(client)
assert(res==nil and head==nil and err=="buffer length less than 4")
res, head, err = ws.read(client)
assert(res==nil and head==nil and err=="buffer length less than 200")
res, head, err = ws.read(client)
assert(res==s..s and head==0x81 and err==nil)
print(ws.read(client))