-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_server_test.rb
74 lines (59 loc) · 1.58 KB
/
new_server_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true
def require_all(directory)
files = Dir["#{directory}/**/*.rb"].sort!
loop do
failed = []
first_name_error = nil
files.each do |file|
begin
require_relative file
rescue NameError => e
failed << file
first_name_error ||= e
end
end
if failed.size == files.size
raise first_name_error
else
files = failed
end
break if failed.empty?
end
end
require "socket"
require_relative "lib/networking"
require_all "lib/networking/backend"
Thread.abort_on_exception = true
server = CyberarmEngine::Networking::Server.new
def server.peer_connected(peer:)
puts "Peer connected: #{peer.id}"
end
def server.packet_received(peer:, message:, channel:)
pp "Server received: #{message} [on channel: #{channel} from peer: #{peer&.id}]"
broadcast_packet(message: "Broadcasting...")
end
Thread.new do
server.bind
loop do
server.update
sleep (1000.0 / 60.0) / 10.0
end
end
connection = CyberarmEngine::Networking::Connection.new(hostname: "localhost", port: CyberarmEngine::Networking::DEFAULT_SERVER_PORT, channels: 3)
def connection.connected
puts "Connection: Connected!"
send_packet(message: "I be connected!")
end
def connection.disconnected(reason:)
puts "Connection: disconnected: #{reason}"
end
def connection.packet_received(message:, channel:)
pp "Connection received: #{message} [on channel: #{channel} from peer: SERVER]"
send_packet(message: "ECHO: #{message}")
end
connection.connect(timeout: 1_000)
loop do
connection.update
sleep (1000.0 / 60.0) / 10.0
end
sleep