forked from vadv/gopher-lua-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
51 lines (44 loc) · 1002 Bytes
/
example_test.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
package tcp
import (
"log"
"time"
lua "github.com/yuin/gopher-lua"
)
// tcp.open(), tcp_client_ud:write(), tcp_client_ud:read()
func Example_full() {
state := lua.NewState()
Preload(state)
go runPingPongServer(":12346")
time.Sleep(time.Second)
source := `
local tcp = require("tcp")
local conn, err = tcp.open(":12346")
if err then error(err) end
-- send ping, read "pong\n"
local err = conn:write("ping")
if err then error(err) end
local result, err = conn:read()
if err then error(err) end
print(result)
-- send ping, read by byte
local err = conn:write("ping")
if err then error(err) end
for i = 1, 5 do
local result, err = conn:read(1)
if err then error(err) end
print(result)
end
conn:close()
`
if err := state.DoString(source); err != nil {
log.Fatal(err.Error())
}
// Output:
// pong
//
// p
// o
// n
// g
//
}