Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Julia-如何通过WebSockets订阅_Websocket_Julia - Fatal编程技术网

Julia-如何通过WebSockets订阅

Julia-如何通过WebSockets订阅,websocket,julia,Websocket,Julia,我想使用Julia订阅一些使用Websockets的数据提要 例如,从linux终端,我可以成功获得如下数据: wscat -c wss://www.bitmex.com/realtime {"op": "subscribe", "args": ["orderBookL2_25:XBTUSD"]} 现在在朱莉娅身上,我找不到解决办法。我尝试了以下方法,但它崩溃了: using WebSockets, JSON u

我想使用Julia订阅一些使用Websockets的数据提要

例如,从linux终端,我可以成功获得如下数据:

wscat -c wss://www.bitmex.com/realtime
{"op": "subscribe", "args": ["orderBookL2_25:XBTUSD"]}
现在在朱莉娅身上,我找不到解决办法。我尝试了以下方法,但它崩溃了:

using WebSockets, JSON

uri = "wss://www.bitmex.com/realtime"
json_part = "{'op': 'subscribe', 'args': ['orderBookL2_25:XBTUSD']}"

inbox = Channel{String}(10)
outbox = Channel{String}(10)

ws_task = @async WebSockets.open(uri) do ws
  while isopen(ws)
        inbox_task = @async while !eof(ws)
            put!(inbox, String(read(ws)))
        end
        outbox_task = @async while isopen(ws)
            write(ws, take!(outbox))
        end
    end
end

# here Julia is crashing (hangs forever, I cannot get the cursor back)

put!(outbox, json_part)
take!(inbox)

有人能帮你找到一个有效的解决方案来使用Julia订阅数据提要吗?

只需在执行
循环时删除外部
,你就可以开始了:

julia> using WebSockets, JSON

julia> uri = "wss://www.bitmex.com/realtime"
"wss://www.bitmex.com/realtime"

julia> json_part = "{'op': 'subscribe', 'args': ['orderBookL2_25:XBTUSD']}"
"{'op': 'subscribe', 'args': ['orderBookL2_25:XBTUSD']}"

julia> inbox = Channel{String}(10)
Channel{String}(sz_max:10,sz_curr:0)

julia> outbox = Channel{String}(10)
Channel{String}(sz_max:10,sz_curr:0)

julia> ws_task = @async WebSockets.open(uri) do ws
           inbox_task = @async while !eof(ws)
               put!(inbox, String(read(ws)))
           end
           outbox_task = @async while isopen(ws)
               write(ws, take!(outbox))
           end
       end
Task (runnable) @0x00000000135b3990

julia> put!(outbox, json_part)
"{'op': 'subscribe', 'args': ['orderBookL2_25:XBTUSD']}"

julia> take!(inbox)
"{\"info\":\"Welcome to the BitMEX Realtime API.\",\"version\":\"2020-10-06T22:31:35.000Z\",\"timestamp\":\"2020-10-26T16:56:02.455Z\",\"docs\":\"https://www.bitmex.com/app/wsAPI\",\"limit\":{\"remaining\":38}}"

当isopen(ws)
循环到位时,外部
将持续创建新的
收件箱
/
发件箱任务
s。如果WS-connection断开或发生其他情况,我怀疑您希望重新启动它们,但您需要以不同的方式进行处理。

第一个解决方案是在url内订阅(不总是可能或需要):

第二个解决方案是在打开套接字后订阅

using WebSockets, JSON

uri = "wss://www.bitmex.com/realtime"

payload = Dict(
               :op => "subscribe",
               :args => "trade:XBT"
           )

function open_websocket() 
   WebSockets.open(uri) do ws
     if isopen(ws)
       write(ws, JSON.json(payload))
     end

     while isopen(ws)
       data, success = readguarded(ws)
       if success
         data = JSON.parse(String(data))
         print(data, "\n")
       end
     end

     if !isopen(ws)
       @async open_websocket()
     end

   end
 end
       
@async open_websocket()

非常感谢。我确实得到了与您上面相同的输出,但我仍然没有得到作为目标的连续数据流。
using WebSockets, JSON

uri = "wss://www.bitmex.com/realtime"

payload = Dict(
               :op => "subscribe",
               :args => "trade:XBT"
           )

function open_websocket() 
   WebSockets.open(uri) do ws
     if isopen(ws)
       write(ws, JSON.json(payload))
     end

     while isopen(ws)
       data, success = readguarded(ws)
       if success
         data = JSON.parse(String(data))
         print(data, "\n")
       end
     end

     if !isopen(ws)
       @async open_websocket()
     end

   end
 end
       
@async open_websocket()