R 中的 Websocket [英] Websockets in R

查看:62
本文介绍了R 中的 Websocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法使用以下规范在 R 中建立到 Mtgox websocket 的连接:

I managed to establish a connection in R to Mtgox websocket with following specs:

我使用了从 https://github.com/zeenogee/R 下载的改进的 R 库websocket"-Websockets:

require("websockets")
con = websocket("https://socketio.mtgox.com/mtgox?Currency=USD")

并且连接已成功建立.但是,套接字似乎没有广播.我做了一个简单的函数 f

and the connection was successfully established. However, it seems that the socket is not broadcasting. I made an easy function f

  f = function(con) {
  Print("Test Test!", con)
}

set_callback("receive", f, con)

while(TRUE)
  {
  service(con)
  Sys.sleep(0.05)
  }

每当从 websocket 接收到一些数据时,它应该打印一些文本.但是 websocket 似乎没有触发接收"方法,也没有显示任何内容.代码以无限循环结束,没有输出.

which should print some text whenever some data are received from the websocket. But the websocket doesnt seem to trigger the "receive" method and nothing is displayed. Code ended up with infinite loop with no output.

我知道 websocket 正在工作,所以代码中一定有错误.我是否必须以某种方式ping"套接字才能开始广播?任何人都知道如何让它工作?谢谢!

I know that the websocket is working so there must be a mistake in the code. Do I have to "ping" the socket somehow to start broadcasting? Anyone has anidea how to get it working? Thanks!

推荐答案

首先,你有一个无限循环,因为你已经定义了一个无限循环:

Firstly, you have an infinite loop, because you have defined an infinite loop:

While(TRUE)

值得注意的是,许多 R websocket 实现都利用了这个循环,因此可能不是错误,而是导致您看到的内容的实现细节.

It is worth noting, numerous R websocket implementations leverage this loop, so may not be a bug but rather an implementation detail causing what you are seeing.

看来您需要订阅消息"事件而不是接收"(https://en.bitcoin.it/wiki/MtGox/API/Streaming).

It would appear that you need to subscribe to the 'message' event not 'receive' ( https://en.bitcoin.it/wiki/MtGox/API/Streaming).

在 JavaScript 中(来自 MtGox 规范):

In JavaScript (from MtGox Spec):

conn.on('message', function(data) {
    // Handle incoming data object.
});

或者在 R 中:

set_callback('message',f,con)

失败了...

我还要评论说,也许流正在返回您无法在 R Print 函数中隐式打印的数据.

I would also comment to say, that maybe the stream is returning you data that you are not able to implicitly print in R Print function.

示例:

{
  "op":"remark",
  "message":<MESSAGE FROM THE SERVER>,
  "success":<boolean>
}

如果数据遵循规范中定义的这种格式,您可以检查该数据的解析方式以及返回的op".

If the data follows this format as defined in spec, you may which to examine how that data is being parsed, and the "op" which is being returned.

这篇关于R 中的 Websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆