Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
177 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
ping 1 { | ||
join 1 { | ||
request { | ||
what 0 : string | ||
room 0 : integer | ||
} | ||
response { | ||
sleep 0 : integer | ||
session 0 : integer | ||
host 1 : string | ||
port 2 : integer | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,60 @@ | ||
local skynet = require "skynet" | ||
local snax = require "snax" | ||
|
||
local max_number = 4 | ||
local roomid | ||
local gate | ||
local users = {} | ||
|
||
function accept.update(session, data) | ||
print("room ->", session, data) | ||
gate.post.post(session, "pong") | ||
function accept.update(data) | ||
local session = string.unpack("<L", data) | ||
print("========>", session, data) | ||
for s,v in pairs(users) do | ||
if s~=session then | ||
-- forward to others in room | ||
gate.post.post(s, data) | ||
end | ||
end | ||
end | ||
|
||
function init(udpserver) | ||
function response.join(agent, secret) | ||
local n = 0 | ||
for _ in pairs(users) do | ||
n = n + 1 | ||
end | ||
if n >= max_number then | ||
return false -- max number of room | ||
end | ||
agent = snax.bind(agent, "agent") | ||
local user = { | ||
agent = agent, | ||
key = secret, | ||
session = gate.req.register(skynet.self(), secret), | ||
} | ||
users[user.session] = user | ||
return user.session | ||
end | ||
|
||
function response.leave(session) | ||
users[session] = nil | ||
end | ||
|
||
function response.query(session) | ||
local user = users[session] | ||
-- todo: we can do more | ||
if user then | ||
return user.agent.handle | ||
end | ||
end | ||
|
||
function init(id, udpserver) | ||
roomid = id | ||
gate = snax.bind(udpserver, "udpserver") | ||
local session = gate.req.register(skynet.self(), "XXXX") | ||
end | ||
|
||
function exit() | ||
for _,user in pairs(users) do | ||
gate.req.unregister(user.session) | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
local snax = require "snax" | ||
|
||
-- todo: we can use a gate pool | ||
local host = "127.0.0.1" | ||
local port = 9999 | ||
local udpgate | ||
local rooms = {} | ||
|
||
function response.apply(roomid) | ||
local room = rooms[roomid] | ||
if room == nil then | ||
room = snax.newservice("room", roomid, udpgate.handle) | ||
rooms[roomid] = room | ||
end | ||
return room.handle , host, port | ||
end | ||
|
||
-- todo : close room ? | ||
|
||
function init() | ||
udpgate = snax.newservice("udpserver",host, port) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters