Skip to content

bhftbootcamp/Libwebsockets.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Libwebsockets.jl

Stable Dev Build Status Coverage Registry

Libwebsockets is a Julia wrapper for the libwebsockets library, providing verstile tooling for setting up WebSocket and HTTP clients and servers, managing SSL connections, and handling data efficiently in a variety of applications.

Installation

To install Libwebsockets, simply use the Julia package manager:

] add Libwebsockets

Usage

With Libwebsockets, you can quickly connect to a WebSocket stream in Julia. In just a few lines, set up a client, handle incoming events, and process data.

using Libwebsockets

mutable struct UserData
    callback::Function
end

function ws_callback(wsi::Ptr{Cvoid}, reason::Cint, user::Ptr{Cvoid}, data::Ptr{Cvoid}, len::Csize_t)::Cint
    if reason == LWS_CALLBACK_CLIENT_RECEIVE && data != C_NULL
        ctx = lws_get_context(wsi)
        user_ctx = unsafe_pointer_to_objref(lws_context_user(ctx))::UserData
        user_ctx.callback(unsafe_wrap(Vector{UInt8}, Ptr{UInt8}(data), len))
    end
    return Cint(0)
end

function ws_open(callback::Function, addr::String, port::Int, path::String)
    lws_set_log_level(0, C_NULL)

    callback_ptr = @cfunction(ws_callback, Cint,
        (Ptr{Cvoid}, Cint, Ptr{Cvoid}, Ptr{Cvoid}, Csize_t))

    proto = "ws"
    protocols = [
        LwsProtocols(pointer(proto), callback_ptr, 0, 0, 0, C_NULL, 0),
        LwsProtocols(C_NULL, C_NULL, 0, 0, 0, C_NULL, 0)
    ]

    user = UserData(callback)

    ctx_info = LwsContextCreationInfo()
    ctx_info.options   = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT
    ctx_info.port      = CONTEXT_PORT_NO_LISTEN
    ctx_info.user      = pointer_from_objref(user)
    ctx_info.protocols = pointer(protocols)
    ws_ctx = lws_create_context(Ref(ctx_info))

    addr_ref = addr
    path_ref = path
    
    conn_info = LwsClientConnectInfo()
    conn_info.context        = ws_ctx
    conn_info.port           = port
    conn_info.address        = pointer(addr_ref)
    conn_info.path           = pointer(path_ref)
    conn_info.host           = conn_info.address
    conn_info.ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED

    GC.@preserve user proto protocols addr_ref path_ref begin
        lws_client_connect_via_info(Ref(conn_info))
        while true
            lws_service(ws_ctx, 20)
        end
    end
end

ws_open("stream.binance.com", 9443, "/stream?streams=adausdt@depth5@100ms") do message
    println("Received message: ", String(message))
end

Useful Links

Contributing

Contributions to Libwebsockets are welcome! If you encounter a bug, have a feature request, or would like to contribute code, please open an issue or a pull request on GitHub.

About

Julia wrapper for the libwebsockets library

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 6

Languages