kopia lustrzana https://github.com/cirospaciari/socketify.py
fixes, examples and binary update
rodzic
16d686f748
commit
a40d598263
|
@ -0,0 +1,39 @@
|
|||
from socketify import App, AppOptions, OpCode, CompressOptions
|
||||
|
||||
#Number between ok and not ok
|
||||
backpressure = 1024
|
||||
|
||||
# Used for statistics
|
||||
messages = 0
|
||||
message_number = 0
|
||||
|
||||
def ws_open(ws):
|
||||
print('A WebSocket got connected!')
|
||||
# We begin our example by sending until we have backpressure
|
||||
global message_number
|
||||
global messages
|
||||
while (ws.get_buffered_amount() < backpressure):
|
||||
ws.send("This is a message, let's call it %i" % message_number)
|
||||
message_number = message_number + 1
|
||||
messages = messages + 1
|
||||
|
||||
def ws_drain(ws):
|
||||
# Continue sending when we have drained (some)
|
||||
global message_number
|
||||
global messages
|
||||
while (ws.get_buffered_amount() < backpressure):
|
||||
ws.send("This is a message, let's call it %i" % message_number)
|
||||
message_number = message_number + 1
|
||||
messages = messages + 1
|
||||
|
||||
app = App()
|
||||
app.ws("/*", {
|
||||
'compression': CompressOptions.DISABLED,
|
||||
'max_payload_length': 16 * 1024 * 1024,
|
||||
'idle_timeout': 60,
|
||||
'open': ws_open,
|
||||
'drain': ws_drain
|
||||
})
|
||||
app.any("/", lambda res,req: res.end("Nothing to see here!'"))
|
||||
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % (config.port)))
|
||||
app.run()
|
Plik binarny nie jest wyświetlany.
|
@ -45,11 +45,11 @@ macos:
|
|||
cd ../uWebSockets/uSockets && $(AR) rvs uSockets_darwin_arm64.a *.o
|
||||
|
||||
# build CAPI + libsocketify
|
||||
$(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
|
||||
$(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
|
||||
$(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -shared -undefined dynamic_lookup -o ../$(LIBRARY_NAME)_darwin_amd64.so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_darwin_amd64.a ../uWebSockets/uSockets/boringssl/amd64/ssl/libssl.a ../uWebSockets/uSockets/boringssl/amd64/crypto/libcrypto.a ../uWebSockets/uSockets/lsquic/src/liblsquic/liblsquic.a -flto -fPIC -lz -luv
|
||||
|
||||
# build CAPI + libsocketify for arm64 (cross compile)
|
||||
$(CXX) -stdlib=libc++ -target arm64-apple-macos11 -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
|
||||
$(CXX) -stdlib=libc++ -target arm64-apple-macos11 -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
|
||||
$(CXX) -stdlib=libc++ -target arm64-apple-macos11 -shared -undefined dynamic_lookup -o ../$(LIBRARY_NAME)_darwin_arm64.so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_darwin_amd64.a ../uWebSockets/uSockets/boringssl/amd64/ssl/libssl.a ../uWebSockets/uSockets/boringssl/amd64/crypto/libcrypto.a ../uWebSockets/uSockets/lsquic/src/liblsquic/liblsquic.a -flto -fPIC -lz -luv
|
||||
|
||||
linux:
|
||||
|
@ -67,5 +67,5 @@ linux:
|
|||
cd ../uWebSockets/uSockets && $(AR) rvs uSockets_linux_$(ARCH).a *.o
|
||||
|
||||
# build CAPI + libsocketify
|
||||
$(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
|
||||
$(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
|
||||
$(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a ../uWebSockets/uSockets/lsquic/src/liblsquic/liblsquic.a -flto -fPIC -lz -luv
|
|
@ -221,6 +221,11 @@ void uws_res_on_data(int ssl, uws_res_t *res, void (*handler)(uws_res_t *res, co
|
|||
void uws_res_upgrade(int ssl, uws_res_t *res, void *data, const char *sec_web_socket_key, size_t sec_web_socket_key_length, const char *sec_web_socket_protocol, size_t sec_web_socket_protocol_length, const char *sec_web_socket_extensions, size_t sec_web_socket_extensions_length, uws_socket_context_t *ws);
|
||||
uws_try_end_result_t uws_res_try_end(int ssl, uws_res_t *res, const char *data, size_t length, uintmax_t total_size, bool close_connection);
|
||||
void uws_res_cork(int ssl, uws_res_t *res,void(*callback)(uws_res_t *res, void* user_data) ,void* user_data);
|
||||
size_t uws_res_get_remote_address(int ssl, uws_res_t *res, const char **dest);
|
||||
size_t uws_res_get_remote_address_as_text(int ssl, uws_res_t *res, const char **dest);
|
||||
size_t uws_res_get_proxied_remote_address(int ssl, uws_res_t *res, const char **dest);
|
||||
size_t uws_res_get_proxied_remote_address_as_text(int ssl, uws_res_t *res, const char **dest);
|
||||
|
||||
bool uws_req_is_ancient(uws_req_t *res);
|
||||
bool uws_req_get_yield(uws_req_t *res);
|
||||
void uws_req_set_field(uws_req_t *res, bool yield);
|
||||
|
@ -246,6 +251,15 @@ uws_sendstatus_t uws_ws_send_last_fragment(int ssl, uws_websocket_t *ws, const c
|
|||
void uws_ws_end(int ssl, uws_websocket_t *ws, int code, const char *message, size_t length);
|
||||
void uws_ws_cork(int ssl, uws_websocket_t *ws, void (*handler)(void *user_data), void *user_data);
|
||||
|
||||
bool uws_ws_subscribe(int ssl, uws_websocket_t *ws, const char *topic, size_t length);
|
||||
bool uws_ws_unsubscribe(int ssl, uws_websocket_t *ws, const char *topic, size_t length);
|
||||
bool uws_ws_is_subscribed(int ssl, uws_websocket_t *ws, const char *topic, size_t length);
|
||||
void uws_ws_iterate_topics(int ssl, uws_websocket_t *ws, void (*callback)(const char *topic, size_t length, void *user_data), void *user_data);
|
||||
bool uws_ws_publish(int ssl, uws_websocket_t *ws, const char *topic, size_t topic_length, const char *message, size_t message_length);
|
||||
bool uws_ws_publish_with_options(int ssl, uws_websocket_t *ws, const char *topic, size_t topic_length, const char *message, size_t message_length, uws_opcode_t opcode, bool compress);
|
||||
int uws_ws_get_buffered_amount(int ssl, uws_websocket_t *ws);
|
||||
size_t uws_ws_get_remote_address(int ssl, uws_websocket_t *ws, const char **dest);
|
||||
size_t uws_ws_get_remote_address_as_text(int ssl, uws_websocket_t *ws, const char **dest);
|
||||
""")
|
||||
|
||||
library_extension = "dll" if platform.system().lower() == "windows" else "so"
|
||||
|
@ -1194,6 +1208,49 @@ class AppResponse:
|
|||
self.cork(lambda res: res.end(message, end_connection))
|
||||
return self
|
||||
|
||||
def get_remote_address_bytes(self):
|
||||
buffer = ffi.new("char**")
|
||||
length = lib.uws_res_get_remote_address(self.SSL, self.res, buffer)
|
||||
buffer_address = ffi.addressof(buffer, 0)[0]
|
||||
if buffer_address == ffi.NULL:
|
||||
return None
|
||||
try:
|
||||
return ffi.unpack(buffer_address, length)
|
||||
except Exception: #invalid
|
||||
return None
|
||||
|
||||
def get_remote_address(self):
|
||||
buffer = ffi.new("char**")
|
||||
length = lib.uws_res_get_remote_address_as_text(self.SSL, self.res, buffer)
|
||||
buffer_address = ffi.addressof(buffer, 0)[0]
|
||||
if buffer_address == ffi.NULL:
|
||||
return None
|
||||
try:
|
||||
return ffi.unpack(buffer_address, length).decode("utf-8")
|
||||
except Exception: #invalid utf-8
|
||||
return None
|
||||
|
||||
def get_proxied_remote_address_bytes(self):
|
||||
buffer = ffi.new("char**")
|
||||
length = lib.uws_res_get_proxied_remote_address(self.SSL, self.res, buffer)
|
||||
buffer_address = ffi.addressof(buffer, 0)[0]
|
||||
if buffer_address == ffi.NULL:
|
||||
return None
|
||||
try:
|
||||
return ffi.unpack(buffer_address, length)
|
||||
except Exception: #invalid
|
||||
return None
|
||||
|
||||
def get_proxied_remote_address(self):
|
||||
buffer = ffi.new("char**")
|
||||
length = lib.uws_res_get_proxied_remote_address_as_text(self.SSL, self.res, buffer)
|
||||
buffer_address = ffi.addressof(buffer, 0)[0]
|
||||
if buffer_address == ffi.NULL:
|
||||
return None
|
||||
try:
|
||||
return ffi.unpack(buffer_address, length).decode("utf-8")
|
||||
except Exception: #invalid utf-8
|
||||
return None
|
||||
def end(self, message, end_connection=False):
|
||||
try:
|
||||
if self.aborted:
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 4536669a85a8cd4ba72e489f33f44a57ff825cfd
|
||||
Subproject commit 25fdcd68d9656009ba75fe28168121ab1d36f7a7
|
|
@ -3,8 +3,9 @@
|
|||
|
||||
# import os.path
|
||||
|
||||
# DLL_EXPORT typedef void (*uws_listen_domain_handler)(struct us_listen_socket_t *listen_socket, const char* domain, size_t domain_length, int options, void *user_data);
|
||||
|
||||
# DLL_EXPORT typedef void (*uws_listen_domain_handler)(struct us_listen_socket_t *listen_socket, const char* domain, size_t domain_length, int options, void *user_data);
|
||||
# DLL_EXPORT typedef void (*uws_filter_handler)(uws_res_t *response, int, void *user_data);
|
||||
|
||||
# DLL_EXPORT void uws_app_listen_domain(int ssl, uws_app_t *app, const char *domain,size_t server_name_length,_listen_domain_handler handler, void *user_data);
|
||||
# DLL_EXPORT void uws_app_listen_domain_with_options(int ssl, uws_app_t *app, const char *domain,size_t servere_length, int options, uws_listen_domain_handler handler, void *user_data);
|
||||
# DLL_EXPORT void uws_app_domain(int ssl, uws_app_t *app, const char* server_name, size_t server_name_length);
|
||||
|
@ -41,4 +42,5 @@ app.ws("/*", {
|
|||
})
|
||||
app.any("/", lambda res,req: res.end("Nothing to see here!'"))
|
||||
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % (config.port)))
|
||||
|
||||
app.run()
|
Ładowanie…
Reference in New Issue