JuliaWeb / WebSockets.jl

A WebSockets library for Julia
MIT License
157 stars 58 forks source link

Is it possible to parse bytes to JSON? #117

Closed TheOnlyArtz closed 5 years ago

TheOnlyArtz commented 5 years ago

I got the next code:


import OpenTrick
import WebSockets

io = OpenTrick.opentrick(WebSockets.open, "wss://gateway.discord.gg/?v=6&encoding=json")

### How would I parse the bytes to JSON?
hustf commented 5 years ago

It is possible, if I understand correctly. You need of couse whole strings to do it. Is your application really speed sensitive, or are you ok with waiting for a few microseconds every message?

Den tor. 27. sep. 2018, 17:12 skrev Amit Katz notifications@github.com:

I got the next code:

import OpenTrick import WebSockets

io = OpenTrick.opentrick(WebSockets.open, "wss://gateway.discord.gg/?v=6&encoding=json")

How would I parse the bytes to JSON?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/JuliaWeb/WebSockets.jl/issues/117, or mute the thread https://github.com/notifications/unsubscribe-auth/ANTatBanafGlqdhx6_KwXgBRqifD6_Mdks5ufOrAgaJpZM4W84YX .

TheOnlyArtz commented 5 years ago

@hustf Hey, thank you for the quick response I've managed to fix it and parse bytes to JSON, but I got another question, I'm trying to keep receiving data from the gateway and I'm trying to do

while isopen(connection)
    println(WebSockets.read(connection))
end

for some reason it never prints and the connection is open (checked by doing println("aaa") inside it.

EDIT: ERROR: LoadError: WebSockets.WebSocketClosedError("ws|client respond to OPCODE_CLOSE 1000:Normal") this what happens when the socket closes (and so does the process)

hustf commented 5 years ago

The information is a little thin here. It's a connection, so both sides depend on what the other side is doing. If both sides read simultaneously, you are stuck for ever. So this can be hard to get right, and for many applications you need some sort of parallelism and a buffer. I would recommend using readguarded instead of reading, though.

Den tor. 27. sep. 2018, 19:58 skrev Amit Katz notifications@github.com:

@hustf https://github.com/hustf Hey, thank you for the quick response I've managed to fix it and parse bytes to JSON, but I got another question, I'm trying to keep receiving data from the gateway and I'm trying to do

while isopen(connection) println(WebSockets.read(connection))end

for some reason it never prints and the connection is open (checked by doing println("aaa") inside it.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/JuliaWeb/WebSockets.jl/issues/117#issuecomment-425186692, or mute the thread https://github.com/notifications/unsubscribe-auth/ANTatGVGcPUhzFZ4yG93ax_RBXZEDTFeks5ufRHLgaJpZM4W84YX .