more example for echo socket service

This commit is contained in:
Cloud Wu
2014-03-14 11:14:03 +08:00
parent c6efe2cd19
commit 5fa3040a85

View File

@@ -3,10 +3,7 @@ local socket = require "socket"
local mode , id = ... local mode , id = ...
if mode == "agent" then local function echo(id)
id = tonumber(id)
skynet.start(function()
socket.start(id) socket.start(id)
while true do while true do
@@ -15,15 +12,22 @@ if mode == "agent" then
socket.write(id, str .. "\n") socket.write(id, str .. "\n")
else else
socket.close(id) socket.close(id)
print("closed " .. id) return
break end
end end
end end
if mode == "agent" then
id = tonumber(id)
skynet.start(function()
skynet.fork(function()
echo(id)
skynet.exit() skynet.exit()
end) end)
end)
else else
local function accepter(id) local function accept(id)
socket.start(id) socket.start(id)
socket.write(id, "Hello Skynet\n") socket.write(id, "Hello Skynet\n")
skynet.newservice("testsocket", "agent", id) skynet.newservice("testsocket", "agent", id)
@@ -37,8 +41,11 @@ else
socket.start(id , function(id, addr) socket.start(id , function(id, addr)
print("connect from " .. addr .. " " .. id) print("connect from " .. addr .. " " .. id)
-- you can also call skynet.newservice for this socket id -- you have choices :
skynet.fork(accepter, id) -- 1. skynet.newservice("testsocket", "agent", id)
-- 2. skynet.fork(echo, id)
-- 3. accept(id)
accept(id)
end) end)
end) end)
end end