TCPSocket 连接被对端重置

TCPSocket connection reset by peer

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 and also csharp/java examples

非常感谢任何帮助。谢谢!

首先,您应该用整数替换字符串“443”:

TCPSocket.open('stream-api.betfair.com', 443)

无论如何,它似乎与 SSL 协商有关:以下 Whosebug post 给出了一个关于什么可行的快速想法: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');