Skip to content

Commit 87e7c8a

Browse files
committed
Improved tcp server example
1 parent 32e4774 commit 87e7c8a

3 files changed

Lines changed: 24 additions & 14 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ Once discovery has been completed communication can take place over a socket of
7373
The `defnet/tcp_server` module can be used to create a TCP socket server that accepts incoming TCP client connections and can send and receive data. Example:
7474

7575
```
76-
local function on_data(data, ip)
76+
local function on_data(data, ip, port, client)
7777
print("Received", data, "from", ip)
7878
return "My response"
7979
end
8080
81-
local function on_client_connected(ip)
81+
local function on_client_connected(ip, port, client)
8282
print("Client", ip, "connected")
8383
end
8484
85-
local function on_client_disconnected(ip)
85+
local function on_client_disconnected(ip, port, client)
8686
print("Client", ip, "disconnected")
8787
end
8888

‎defnet/tcp_server.lua‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
--
55
-- @usage
66
--
7-
-- local function on_data(data, ip)
7+
-- local function on_data(data, ip, port, client)
88
-- print("Received", data, "from", ip)
99
-- return "My response"
1010
-- end
1111
--
12-
-- local function on_client_connected(ip)
12+
-- local function on_client_connected(ip, port, client)
1313
-- print("Client", ip, "connected")
1414
-- end
1515
--
16-
-- local function on_client_disconnected(ip)
16+
-- local function on_client_disconnected(ip, port, client)
1717
-- print("Client", ip, "disconnected")
1818
-- end
1919
--
@@ -69,7 +69,7 @@ function M.create(port, on_data, on_client_connected, on_client_disconnected)
6969
queues[connection_to_remove] = nil
7070
if on_client_disconnected then
7171
local client_ip, client_port = connection:getsockname()
72-
on_client_disconnected(client_ip, client_port)
72+
on_client_disconnected(client_ip, client_port, connection)
7373
end
7474
break
7575
end
@@ -144,7 +144,7 @@ function M.create(port, on_data, on_client_connected, on_client_disconnected)
144144
queues[client] = tcp_send_queue.create(client, M.TCP_SEND_CHUNK_SIZE)
145145
if on_client_connected then
146146
local client_ip, client_port = client:getsockname()
147-
on_client_connected(client_ip, client_port)
147+
on_client_connected(client_ip, client_port, client)
148148
end
149149
end
150150

@@ -155,7 +155,7 @@ function M.create(port, on_data, on_client_connected, on_client_disconnected)
155155
local data, err = server.receive(client)
156156
if data and on_data then
157157
local client_ip, client_port = client:getsockname()
158-
local response = on_data(data, client_ip, client_port, function(response)
158+
local response = on_data(data, client_ip, client_port, client, function(response)
159159
if not queues[client] then
160160
return false
161161
end

‎examples/tcp_server/tcp_server.gui_script‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,27 @@ local dirtylarry = require "gooey.themes.dirtylarry.dirtylarry"
33

44
local TCP_SERVER_PORT = 5555
55

6+
local function on_data(data, ip, port, client)
7+
print("TCP server received data '" .. data .. "' from " .. ip .. ":" .. port)
8+
local logo = gui.get_node("logo")
9+
gui.animate(logo, "rotation.z", gui.get_rotation(logo).z -360, gui.EASING_INOUTEXPO, 2, 0, nil, gui.PLAYBACK_ONCE_FORWARD)
10+
end
11+
12+
local function on_client_connected(ip, port, client)
13+
print("Client connected from " .. ip .. ":" .. port)
14+
end
15+
16+
local function on_client_disconnected(ip, port, client)
17+
print("Client discconnected from " .. ip .. ":" .. port)
18+
end
19+
620
function init(self)
721
msg.post(".", "acquire_input_focus")
822

9-
local logo = gui.get_node("logo")
1023
dirtylarry.set_enabled(dirtylarry.button("stop"), false)
1124
dirtylarry.set_enabled(dirtylarry.button("broadcast"), false)
1225

13-
self.tcp_server = tcp_server.create(TCP_SERVER_PORT, function(data, ip, port)
14-
print("TCP server received data '" .. data .. "' from " .. ip .. ":" .. port)
15-
gui.animate(logo, "rotation.z", gui.get_rotation(logo).z -360, gui.EASING_INOUTEXPO, 2, 0, nil, gui.PLAYBACK_ONCE_FORWARD)
16-
end)
26+
self.tcp_server = tcp_server.create(TCP_SERVER_PORT, on_data, on_client_connected, on_client_disconnected)
1727
end
1828

1929
function final(self)

0 commit comments

Comments
 (0)