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