Skip to content

Commit 1bc8347

Browse files
authored
Update README.md (#24)
1 parent 1976e31 commit 1bc8347

File tree

2 files changed

+52
-38
lines changed

2 files changed

+52
-38
lines changed

README.md

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,52 @@ mutable struct UserData
2727
callback::Function
2828
end
2929

30-
function ws_callback(wsi::Ptr{Cvoid}, reason::Cint, user::Ptr{Cvoid}, data::Ptr{Cvoid}, len::Csize_t)
30+
function ws_callback(wsi::Ptr{Cvoid}, reason::Cint, user::Ptr{Cvoid}, data::Ptr{Cvoid}, len::Csize_t)::Cint
3131
if reason == LWS_CALLBACK_CLIENT_RECEIVE && data != C_NULL
3232
ctx = lws_get_context(wsi)
33-
user_ctx = unsafe_pointer_to_objref(lws_context_user(ctx))
33+
user_ctx = unsafe_pointer_to_objref(lws_context_user(ctx))::UserData
3434
user_ctx.callback(unsafe_wrap(Vector{UInt8}, Ptr{UInt8}(data), len))
3535
end
36-
return lws_callback_http_dummy(wsi, reason, user, data, len)
36+
return Cint(0)
3737
end
3838

3939
function ws_open(callback::Function, addr::String, port::Int, path::String)
4040
lws_set_log_level(0, C_NULL)
41-
42-
callback_ptr = @cfunction(ws_callback, Cint, (Ptr{Cvoid}, Cint, Ptr{Cvoid}, Ptr{Cvoid}, Csize_t))
41+
42+
callback_ptr = @cfunction(ws_callback, Cint,
43+
(Ptr{Cvoid}, Cint, Ptr{Cvoid}, Ptr{Cvoid}, Csize_t))
44+
45+
proto = "ws"
4346
protocols = [
44-
LwsProtocols(pointer("ws"), callback_ptr, 0, 0, 0, C_NULL, 0),
47+
LwsProtocols(pointer(proto), callback_ptr, 0, 0, 0, C_NULL, 0),
4548
LwsProtocols(C_NULL, C_NULL, 0, 0, 0, C_NULL, 0)
4649
]
50+
4751
user = UserData(callback)
4852

4953
ctx_info = LwsContextCreationInfo()
50-
ctx_info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT
51-
ctx_info.port = CONTEXT_PORT_NO_LISTEN
52-
ctx_info.user = Base.unsafe_convert(Ptr{UserData}, Ref(user))
54+
ctx_info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT
55+
ctx_info.port = CONTEXT_PORT_NO_LISTEN
56+
ctx_info.user = pointer_from_objref(user)
5357
ctx_info.protocols = pointer(protocols)
5458
ws_ctx = lws_create_context(Ref(ctx_info))
5559

60+
addr_ref = addr
61+
path_ref = path
62+
5663
conn_info = LwsClientConnectInfo()
57-
conn_info.context = ws_ctx
58-
conn_info.port = port
59-
conn_info.address = pointer(addr)
60-
conn_info.path = pointer(path)
61-
conn_info.host = conn_info.address
64+
conn_info.context = ws_ctx
65+
conn_info.port = port
66+
conn_info.address = pointer(addr_ref)
67+
conn_info.path = pointer(path_ref)
68+
conn_info.host = conn_info.address
6269
conn_info.ssl_connection = LCCSCF_USE_SSL | LCCSCF_ALLOW_SELFSIGNED
63-
lws_client_connect_via_info(Ref(conn_info))
6470

65-
while true
66-
lws_service(ws_ctx, 0)
71+
GC.@preserve user proto protocols addr_ref path_ref begin
72+
lws_client_connect_via_info(Ref(conn_info))
73+
while true
74+
lws_service(ws_ctx, 20)
75+
end
6776
end
6877
end
6978

docs/src/index.md

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,52 @@ mutable struct UserData
2121
callback::Function
2222
end
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)
3131
end
3232

3333
function 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
6271
end
6372

@@ -71,7 +80,3 @@ end
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

Comments
 (0)