forked from cloudwu/skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestsocket.lua
52 lines (45 loc) · 1.06 KB
/
testsocket.lua
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
local skynet = require "skynet"
local socket = require "skynet.socket"
local mode , id = ...
local function echo(id)
socket.start(id)
while true do
local str = socket.read(id)
if str then
socket.write(id, str)
else
socket.close(id)
return
end
end
end
if mode == "agent" then
id = tonumber(id)
skynet.start(function()
skynet.fork(function()
echo(id)
skynet.exit()
end)
end)
else
local function accept(id)
socket.start(id)
socket.write(id, "Hello Skynet\n")
skynet.newservice(SERVICE_NAME, "agent", id)
-- notice: Some data on this connection(id) may lost before new service start.
-- So, be careful when you want to use start / abandon / start .
socket.abandon(id)
end
skynet.start(function()
local id = socket.listen("127.0.0.1", 8001)
print("Listen socket :", "127.0.0.1", 8001)
socket.start(id , function(id, addr)
print("connect from " .. addr .. " " .. id)
-- you have choices :
-- 1. skynet.newservice("testsocket", "agent", id)
-- 2. skynet.fork(echo, id)
-- 3. accept(id)
accept(id)
end)
end)
end