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,27 +3,31 @@ local socket = require "socket"
local mode , id = ... local mode , id = ...
local function echo(id)
socket.start(id)
while true do
local str = socket.readline(id,"\n")
if str then
socket.write(id, str .. "\n")
else
socket.close(id)
return
end
end
end
if mode == "agent" then if mode == "agent" then
id = tonumber(id) id = tonumber(id)
skynet.start(function() skynet.start(function()
socket.start(id) skynet.fork(function()
echo(id)
while true do skynet.exit()
local str = socket.readline(id,"\n") end)
if str then
socket.write(id, str .. "\n")
else
socket.close(id)
print("closed " .. id)
break
end
end
skynet.exit()
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