JuliaWeb / WebSockets.jl

A WebSockets library for Julia
MIT License
158 stars 57 forks source link

Can I keep a client connection open beyond one function call? #113

Closed christopher-dG closed 5 years ago

christopher-dG commented 6 years ago

WebSockets.open takes a function/do block and closes the connection automatically afterwards. Is there currently any way to open a connection, return the WebSocket for later manipulation, and have the ability to close the connection manually when we're done?

I know I could pass in a function which just loops forever but it's not a great solution for what I want to do.

hustf commented 6 years ago

Yes, there is. But the websocket can always be closed by the program on the other side anyway.

The websocket closes when you exit that function. If you never exit it, every call will fill up memory.

There is currently a possible problem in Julia 0.7 or 1.0 with responding in a parallel worker process and passing the reference back. But Julia 0.6 benchmarks do that.

Den tor. 13. sep. 2018, 07:47 skrev Chris de Graaf <notifications@github.com

:

WebSockets.open takes a function/do block and closes the connection automatically afterwards. Is there currently any way to open a connection, return the WebSocket for later manipulation, and have the ability to close the connection manually when we're done?

I know I could pass in a function which just loops forever but it's not a great solution for what I want to do.

— 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/113, or mute the thread https://github.com/notifications/unsubscribe-auth/ANTatP7ZZr6SvOq4Y8TOFdRuCi7ixaQSks5uafGAgaJpZM4WmnIs .

hustf commented 6 years ago

The examples share server side sockets with other coroutines through mutable collections.

You can do the same thing with client side sockets. I can just not see the use for it. Can you give an example?

hustf commented 6 years ago

Come to think about it, I think it is very reasonable to include a function simply returning an open websocket or throw an error. This is analogous to the way you can open a file, and it makes throwing together a script step by step easier, allowing for inspection on the way. Thanks for the input!

christopher-dG commented 6 years ago

As for my use case, I'm trying to wrap the Discord API. But I did find a middle ground with processing the connection in a coroutine, so this feature request is not so urgent anymore. I do still agree that it would be useful though :slightly_smiling_face:.

zhanglix commented 6 years ago

I wrote a module OpenTrick to make interactive playing with WebSocket server easier.

using OpenTrick
using WebSockets

io = opentrick(WebSockets.open, "ws://echo.websocket.org");
write(io, "Hello");
println(String(read(io)));

close(io)  # you can close io manually
io = nothing; # or leave it to GC
unsafe_clear() # or you can clear all ios opened by opentrick manually
christopher-dG commented 6 years ago

Awesome, I'll probably give it a try :slightly_smiling_face:

hustf commented 5 years ago

I am closing this issue, and would also recommend a look at this alternative approach:

inbox_outbox.jl