mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 04:33:05 +00:00
support socket.abandon / socket.start , Issue #73
This commit is contained in:
@@ -1,27 +1,44 @@
|
||||
local skynet = require "skynet"
|
||||
local socket = require "socket"
|
||||
|
||||
local function accepter(id)
|
||||
socket.start(id)
|
||||
socket.write(id, "Hello Skynet\n")
|
||||
while true do
|
||||
local str = socket.readline(id,"\n")
|
||||
if str then
|
||||
socket.write(id, str .. "\n")
|
||||
else
|
||||
socket.close(id)
|
||||
print("closed " .. id)
|
||||
return
|
||||
local mode , id = ...
|
||||
|
||||
if mode == "agent" then
|
||||
id = tonumber(id)
|
||||
|
||||
skynet.start(function()
|
||||
socket.start(id)
|
||||
|
||||
while true do
|
||||
local str = socket.readline(id,"\n")
|
||||
if str then
|
||||
socket.write(id, str .. "\n")
|
||||
else
|
||||
socket.close(id)
|
||||
print("closed " .. id)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
local id = socket.listen("127.0.0.1", 8000)
|
||||
|
||||
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)
|
||||
skynet.exit()
|
||||
end)
|
||||
end)
|
||||
else
|
||||
local function accepter(id)
|
||||
socket.start(id)
|
||||
socket.write(id, "Hello Skynet\n")
|
||||
skynet.newservice("testsocket", "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", 8000)
|
||||
|
||||
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)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
Reference in New Issue
Block a user