@@ -21,43 +21,52 @@ mutable struct UserData
2121 callback:: Function
2222end
2323
24- function ws_callback (wsi:: Ptr{Cvoid} , reason:: Cint , user:: Ptr{Cvoid} , data:: Ptr{Cvoid} , len:: Csize_t )
24+ function ws_callback (wsi:: Ptr{Cvoid} , reason:: Cint , user:: Ptr{Cvoid} , data:: Ptr{Cvoid} , len:: Csize_t ):: Cint
2525 if reason == LWS_CALLBACK_CLIENT_RECEIVE && data != C_NULL
2626 ctx = lws_get_context (wsi)
27- user_ctx = unsafe_pointer_to_objref (lws_context_user (ctx))
27+ user_ctx = unsafe_pointer_to_objref (lws_context_user (ctx)):: UserData
2828 user_ctx. callback (unsafe_wrap (Vector{UInt8}, Ptr {UInt8} (data), len))
2929 end
30- return lws_callback_http_dummy (wsi, reason, user, data, len )
30+ return Cint ( 0 )
3131end
3232
3333function ws_open (callback:: Function , addr:: String , port:: Int , path:: String )
3434 lws_set_log_level (0 , C_NULL )
35-
36- callback_ptr = @cfunction (ws_callback, Cint, (Ptr{Cvoid}, Cint, Ptr{Cvoid}, Ptr{Cvoid}, Csize_t))
35+
36+ callback_ptr = @cfunction (ws_callback, Cint,
37+ (Ptr{Cvoid}, Cint, Ptr{Cvoid}, Ptr{Cvoid}, Csize_t))
38+
39+ proto = " ws"
3740 protocols = [
38- LwsProtocols (pointer (" ws " ), callback_ptr, 0 , 0 , 0 , C_NULL , 0 ),
41+ LwsProtocols (pointer (proto ), callback_ptr, 0 , 0 , 0 , C_NULL , 0 ),
3942 LwsProtocols (C_NULL , C_NULL , 0 , 0 , 0 , C_NULL , 0 )
4043 ]
44+
4145 user = UserData (callback)
4246
4347 ctx_info = LwsContextCreationInfo ()
44- ctx_info. options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT
45- ctx_info. port = CONTEXT_PORT_NO_LISTEN
46- ctx_info. user = Base . unsafe_convert (Ptr{UserData}, Ref ( user) )
48+ ctx_info. options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT
49+ ctx_info. port = CONTEXT_PORT_NO_LISTEN
50+ ctx_info. user = pointer_from_objref ( user)
4751 ctx_info. protocols = pointer (protocols)
4852 ws_ctx = lws_create_context (Ref (ctx_info))
4953
54+ addr_ref = addr
55+ path_ref = path
56+
5057 conn_info = LwsClientConnectInfo ()
51- conn_info. context = ws_ctx
52- conn_info. port = port
53- conn_info. address = pointer (addr )
54- conn_info. path = pointer (path )
55- conn_info. host = conn_info. address
58+ conn_info. context = ws_ctx
59+ conn_info. port = port
60+ conn_info. address = pointer (addr_ref )
61+ conn_info. path = pointer (path_ref )
62+ conn_info. host = conn_info. address
5663 conn_info. ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED
57- lws_client_connect_via_info (Ref (conn_info))
5864
59- while true
60- lws_service (ws_ctx, 0 )
65+ GC. @preserve user proto protocols addr_ref path_ref begin
66+ lws_client_connect_via_info (Ref (conn_info))
67+ while true
68+ lws_service (ws_ctx, 20 )
69+ end
6170 end
6271end
6372
7180- [ Libwebsockets] ( https://github.com/warmcat/libwebsockets ) – Official library repository.
7281- [ libwebsockets_org] ( https://libwebsockets.org/ ) – Library documentation.
7382- [ libwebsockets_jll] ( https://github.com/JuliaBinaryWrappers/libwebsockets_jll.jl ) – Julia wrapper for Libwebsockets.
74-
75- ## Contributing
76-
77- 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.
0 commit comments