I have been reading up on TCPSockets over at TutorialsPoint and I need a simple client to make requests but I do not confused why there is no response.
Website API Login
The login command accepts a JSON object as argument followed by the EOT character for a response.
From the website's API
This is what I have
def connect
url = "someurl.com"
port = 19534
loginRequest = 'login {"protocol":1,"client":"testruby","clientver":0.1}' +"\04"
s = TCPSocket.open(url,port)
s.puts(loginRequest)
while line = s.gets
puts line.chop
end
puts "end"
s.close
end
connect
My attempts
1) I run the ruby code in terminal but my connection to the server is immediately closed.
I'm totally clueless and would appreciate some directions on this.
It seems that the server is using raw TCP/IP, using the 0x04 character to signify an "End of Message".
Your code seems to prefer blocking IO (although it probably wouldn't be my first choice), so allow me to suggest a blocking API adjustment:
require 'socket'
module VNDB
public
def send_message message
raise "Not connected" unless @s
@s.write (message + "\x04")
end
def get_message
raise "Not connected" unless @s
message = ''
message << @s.getc until message[-1] == "\x04"
message
end
def connect
url = "someurl.com"
port = 19534
loginRequest = 'login {"protocol":1,"client":"testruby","clientver":0.1}' +"\04"
@s = TCPSocket.open(url,port)
send_message loginRequest
puts get_message
puts "end"
@s.close
@s = nil
end
extend self
end
VNDB.connect
PS
I couldn't test the code, because "someurl.com" isn't really the address, I guess.
The database server is NOT using Websockets (which is a name of a protocol), so please consider editing your question and removing that tag.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.