mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-22 02:53:09 +00:00
rewrite makefile, and remove luacode cache.
This commit is contained in:
51
test/testsocket.lua
Normal file
51
test/testsocket.lua
Normal file
@@ -0,0 +1,51 @@
|
||||
local skynet = require "skynet"
|
||||
local socket = require "socket"
|
||||
|
||||
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
|
||||
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("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 have choices :
|
||||
-- 1. skynet.newservice("testsocket", "agent", id)
|
||||
-- 2. skynet.fork(echo, id)
|
||||
-- 3. accept(id)
|
||||
accept(id)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
Reference in New Issue
Block a user