-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathconnection.rb
More file actions
39 lines (34 loc) · 889 Bytes
/
connection.rb
File metadata and controls
39 lines (34 loc) · 889 Bytes
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
require 'socket'
require_relative 'proto'
class Connection
def initialize(host)
puts "Connecting to #{host}"
@conn = TCPSocket.new host, 9933
@log = Logger.new("network.log")
end
def read
payload = SmartFox2X::SFSPacket.read(@conn).payload
response = payload.convert
@log.debug("<=== #{response.inspect}")
response
end
def write(packet)
raw = packet.to_binary_s
payload = SmartFox2X::SFSPacket.read(StringIO.new(raw)).payload
request = payload.convert
@log.debug("===> #{request.inspect}")
@conn.write(raw)
@conn.flush
end
def waitfor(name)
begin
response = read
end until response["p"]["c"] == name
response
end
def close
puts "Closing connection"
@conn.close
@log.close
end
end