require 'socket'
socket = TCPSocket.open('stream-api.betfair.com', '443')
while line = socket.gets
puts line.chop
end
socket.close
I should receive像 {"op":"connection","connectionId":"002-230915140112-174"}
但是我收到了 Connection reset by peer
means the remote end would have sent a reset packet (RST) to kill the connection without an orderly shutdown (close). In that case you know it was the peer(client).
包括必发 a nodejs example还有csharp/java examples
非常感谢任何帮助。谢谢!
最佳答案
首先,您应该用整数替换字符串“443”:
TCPSocket.open('stream-api.betfair.com', 443)
无论如何,它似乎与 SSL 协商有关:以下 Stackoverflow 帖子给出了一个关于什么可行的快速想法:How to establish a SSL enabled TCP/IP Connection in Ruby .使用这种方法,我工作了。
require 'socket'
require 'openssl'
host = 'stream-api.betfair.com'
port = 443
socket = TCPSocket.open(host,port)
ssl_context = OpenSSL::SSL::SSLContext.new()
ssl_context.ssl_version = :SSLv23
ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
ssl_socket.sync_close = true
ssl_socket.connect
while line = ssl_socket.gets
p line
end
ssl_socket.close
结果:
"{\"op\":\"connection\",\"connectionId\":\"001-151118094105-259478\"}\r\n"
处理 SSL/TLS 保护的连接有时在 Ruby 中非常冗长。在您给出的示例中,在 NodeJS 中,提示是第一行:
var tls = require('tls');
关于ruby - TCPSocket 连接被对端重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53315331/