Websocket version 1.0 simple examples for local host


#1

Can someone please give me an easy example of this?


#2

Maybe this one

julia> using Sockets

julia> using JSON

julia> pipe = ""

julia> if Sys.iswindows()
           pipe = "\\\\.\\pipe\\testsocket"
       else
           pipe = tempname()
       end
"/var/folders/3z/1kq4lzyd39l1c2gl5c0pzvy80000gp/T/julia0Q6Mrr"

julia> @async begin
           server = listen(pipe)
           while true
               sock = accept(server)
               @async while isopen(sock)
                   input = JSON.parse(sock)
                   JSON.print(sock, Dict("data" => rand(input["input"])))
              end
           end
       end
Task (runnable) @0x000000010b2a61d0

julia> clientside = connect(pipe)
Base.PipeEndpoint(RawFD(0x00000015) open, 0 bytes waiting)

julia> for i in 1:10
           JSON.print(clientside,Dict("input" => i))
           println(JSON.parse(clientside))
       end
Dict{String,Any}("data"=>Any[0.559774])
Dict{String,Any}("data"=>Any[0.89733, 0.513331])
Dict{String,Any}("data"=>Any[0.0675964, 0.381228, 0.618926])
Dict{String,Any}("data"=>Any[0.961195, 0.645998, 0.696614, 0.539933])
Dict{String,Any}("data"=>Any[0.927968, 0.328774, 0.969805, 0.959731, 0.169076])
Dict{String,Any}("data"=>Any[0.33247, 0.929461, 0.162861, 0.270243, 0.827589, 0.666108])
Dict{String,Any}("data"=>Any[0.0766846, 0.996563, 0.299863, 0.996271, 0.904049, 0.622786, 0.315614])
Dict{String,Any}("data"=>Any[0.245844, 0.991654, 0.590529, 0.515608, 0.557779, 0.331809, 0.0752659, 0.205465])
Dict{String,Any}("data"=>Any[0.841952, 0.514019, 0.676124, 0.668515, 0.72262, 0.231272, 0.425817, 0.512281, 0.949894])
Dict{String,Any}("data"=>Any[0.360244, 0.484267, 0.946671, 0.162157, 0.569451, 0.416769, 0.433126, 0.0784071, 0.432733, 0.521421])

julia> 

#3

While that could work for basic TCP socket communications, as far as I’m aware that is not a true websocket.
A websocket is a HTTP request, which then gets upgraded to be a websocket, allowing data to be passed. That data is still encapsulated to be messages, instead of just a byte stream without boundaries and is identified to be either JSON or binary data (I’m not sure if other formats are also supported).

Your minimal example has no HTTP connect request which gets upgraded to a websocket, thus it will not be able to interface to a compliant websocket application which only supports websockets and not sockets in general on example might be javascript running in a browser.

When I looked at Websocket support in Sep/Oct 2018 I briefly looked at https://github.com/JuliaWeb/WebSockets.jl however I only had limited success. From what I remember, I launched the async server, but had it crash when a connection was being established and I couldn’t figure out how to properly debug that, since it was running in a separate async. After the fact, my guess is I was setting the websocket settings incorrectly from the browser side, since it was my first time using websockets.

I settled for a solution of using a standard TCP connection to an external C based (gateway) server, which then acted as the Websocket server using https://libwebsockets.org but there are numerous other libraries available. This felt clumsy, but does allow me some flexibilty, in for example being able to act as a gateway to allow connections to multiple Julia processes from a single webserver port, which turned out to be useful in my application. (Granted this gateway server could still have been or in future be upgraded to a Julia server).


#4

Hello,
I’m maintaining https://github.com/juliaweb/WebSockets.jl/. It is currently on version 1.2.0, so you may be using one of the other implementations out here?

I believe examples are in a good state, and the printed output is certainly more user friendly than when Iwabeke tried; this is our best attempt at making examples ‘simple’, provided that you will want detail control of when you lose connections or make your own coding errors. Error messages from run-time client connections are available on server channels.

That simplicity in examples means the opposite of plug-and-play-simplicity. Those longer examples tend to grow in size very quickly, and become hard to maintain over time given how much Julia and Http.jl have been changing. With ground-level stability, it will become more tempting to share ‘real-life-examples’.