-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.rb
55 lines (46 loc) · 893 Bytes
/
example.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
require 'eventmachine'
load 'packet.rb'
class Foo < Packet
id 3
int "a"
float "bsdf"
end
class LoginRequest < Packet
id 1
string "user_id", 32
string "user_pw", 32
end
class LoginResponse < Packet
id 4
int "result"
string "user_nickname", 32
end
module TestClient
def post_init
puts "connected"
f = Foo.new
f.a = 5
send_data f.serialize
a = LoginRequest.new
a.user_id = "pjc0247"
a.user_pw = "asdf1234"
send_data a.serialize
end
def unbind
puts "disconnected"
end
def receive_data data
size, id = data.unpack("II")
case id
when 3
f = Foo.unserialize data
puts "foo result #{f.a}"
when 2
r = LoginResponse.unserialize data
puts "login result #{r.result} / #{r.user_nickname}"
end
end
end
EventMachine.run {
EventMachine.connect "localhost", 9916, TestClient
}