mirror of
https://github.com/ading2210/libcurl.js.git
synced 2025-05-13 14:30:02 -04:00
add old v0.4 code
This commit is contained in:
parent
582bbd4ecd
commit
9205ae1507
14 changed files with 340 additions and 180 deletions
|
@ -1,38 +1,30 @@
|
|||
//class for custom websocket
|
||||
|
||||
class CurlWebSocket extends EventTarget {
|
||||
constructor(url, protocols=[], websocket_debug=false) {
|
||||
super();
|
||||
class CurlWebSocket extends CustomWebSocket {
|
||||
constructor(url, protocols=[], debug=false) {
|
||||
super(url, protocols);
|
||||
check_loaded(true);
|
||||
if (!url.startsWith("wss://") && !url.startsWith("ws://")) {
|
||||
throw new SyntaxError("invalid url");
|
||||
}
|
||||
|
||||
this.url = url;
|
||||
|
||||
this.protocols = protocols;
|
||||
this.binaryType = "blob";
|
||||
this.debug = debug;
|
||||
this.recv_buffer = [];
|
||||
this.websocket_debug = websocket_debug;
|
||||
|
||||
//legacy event handlers
|
||||
this.onopen = () => {};
|
||||
this.onerror = () => {};
|
||||
this.onmessage = () => {};
|
||||
this.onclose = () => {};
|
||||
|
||||
this.CONNECTING = 0;
|
||||
this.OPEN = 1;
|
||||
this.CLOSING = 2;
|
||||
this.CLOSED = 3;
|
||||
|
||||
this.connect();
|
||||
}
|
||||
|
||||
connect() {
|
||||
this.status = this.CONNECTING;
|
||||
let data_callback = () => {};
|
||||
let finish_callback = (error, response_info) => {
|
||||
this.finish_callback(error, response_info);
|
||||
if (error === 0) {
|
||||
this.status = this.OPEN;
|
||||
this.open_callback();
|
||||
this.recv_loop();
|
||||
}
|
||||
else {
|
||||
this.status = this.CLOSED;
|
||||
this.cleanup(error);
|
||||
}
|
||||
}
|
||||
let options = {};
|
||||
if (this.protocols) {
|
||||
|
@ -40,23 +32,23 @@ class CurlWebSocket extends EventTarget {
|
|||
"Sec-Websocket-Protocol": this.protocols.join(", "),
|
||||
};
|
||||
}
|
||||
if (this.websocket_debug) {
|
||||
if (this.debug) {
|
||||
options._libcurl_verbose = 1;
|
||||
}
|
||||
this.http_handle = perform_request(this.url, options, data_callback, finish_callback, null);
|
||||
this.recv_loop();
|
||||
}
|
||||
|
||||
recv() {
|
||||
custom_recv() {
|
||||
let buffer_size = 64*1024;
|
||||
let result_ptr = _recv_from_websocket(this.http_handle, buffer_size);
|
||||
let data_ptr = _get_result_buffer(result_ptr);
|
||||
let result_code = _get_result_code(result_ptr);
|
||||
|
||||
if (result_code == 0) { //CURLE_OK - data recieved
|
||||
if (result_code == 0) { //CURLE_OK - data received
|
||||
if (_get_result_closed(result_ptr)) {
|
||||
//this.pass_buffer();
|
||||
this.close_callback();
|
||||
_free(data_ptr);
|
||||
_free(result_ptr);
|
||||
this.cleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -69,133 +61,43 @@ class CurlWebSocket extends EventTarget {
|
|||
let full_data = merge_arrays(this.recv_buffer);
|
||||
let is_text = _get_result_is_text(result_ptr)
|
||||
this.recv_buffer = [];
|
||||
this.recv_callback(full_data, is_text);
|
||||
return {
|
||||
success: true,
|
||||
data: full_data,
|
||||
is_text: is_text
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (result_code == 52) { //CURLE_GOT_NOTHING - socket closed
|
||||
this.close_callback();
|
||||
this.cleanup();
|
||||
}
|
||||
|
||||
_free(data_ptr);
|
||||
_free(result_ptr);
|
||||
}
|
||||
|
||||
recv_loop() {
|
||||
this.event_loop = setInterval(() => {
|
||||
this.recv();
|
||||
}, 1);
|
||||
}
|
||||
|
||||
recv_callback(data, is_text=false) {
|
||||
let converted;
|
||||
if (is_text) {
|
||||
converted = new TextDecoder().decode(data);
|
||||
return {
|
||||
success: false,
|
||||
data: null,
|
||||
is_text: false
|
||||
}
|
||||
else {
|
||||
if (this.binaryType == "blob") {
|
||||
converted = new Blob(data);
|
||||
}
|
||||
else if (this.binaryType == "arraybuffer") {
|
||||
converted = data.buffer;
|
||||
}
|
||||
else {
|
||||
throw "invalid binaryType string";
|
||||
}
|
||||
}
|
||||
|
||||
let msg_event = new MessageEvent("message", {data: converted});
|
||||
this.onmessage(msg_event);
|
||||
this.dispatchEvent(msg_event);
|
||||
}
|
||||
|
||||
close_callback(error=false) {
|
||||
if (this.status == this.CLOSED) return;
|
||||
this.status = this.CLOSED;
|
||||
|
||||
cleanup(error=false) {
|
||||
if (this.http_handle) _cleanup_handle(this.http_handle);
|
||||
clearInterval(this.event_loop);
|
||||
_cleanup_websocket();
|
||||
|
||||
if (error) {
|
||||
let error_event = new Event("error");
|
||||
this.dispatchEvent(error_event);
|
||||
this.onerror(error_event);
|
||||
}
|
||||
else {
|
||||
let close_event = new CloseEvent("close");
|
||||
this.dispatchEvent(close_event);
|
||||
this.onclose(close_event);
|
||||
}
|
||||
this.close_callback(error);
|
||||
}
|
||||
|
||||
finish_callback(error, response_info) {
|
||||
this.status = this.OPEN;
|
||||
if (error != 0) this.close_callback(true);
|
||||
let open_event = new Event("open");
|
||||
this.onopen(open_event);
|
||||
this.dispatchEvent(open_event);
|
||||
}
|
||||
|
||||
send(data) {
|
||||
let is_text = false;
|
||||
if (this.status === this.CONNECTING) {
|
||||
throw new DOMException("ws not ready yet");
|
||||
}
|
||||
if (this.status === this.CLOSED) {
|
||||
return;
|
||||
}
|
||||
|
||||
let data_array;
|
||||
if (typeof data === "string") {
|
||||
data_array = new TextEncoder().encode(data);
|
||||
is_text = true;
|
||||
}
|
||||
else if (data instanceof Blob) {
|
||||
data.arrayBuffer().then(array_buffer => {
|
||||
data_array = new Uint8Array(array_buffer);
|
||||
this.send(data_array);
|
||||
});
|
||||
return;
|
||||
}
|
||||
//any typedarray
|
||||
else if (data instanceof ArrayBuffer) {
|
||||
//dataview objects
|
||||
if (ArrayBuffer.isView(data) && data instanceof DataView) {
|
||||
data_array = new Uint8Array(data.buffer);
|
||||
}
|
||||
//regular arraybuffers
|
||||
else {
|
||||
data_array = new Uint8Array(data);
|
||||
}
|
||||
}
|
||||
//regular typed arrays
|
||||
else if (ArrayBuffer.isView(data)) {
|
||||
data_array = Uint8Array.from(data);
|
||||
}
|
||||
else {
|
||||
throw "invalid data type to be sent";
|
||||
}
|
||||
|
||||
custom_send(data_array, is_text) {
|
||||
let data_ptr = allocate_array(data_array);
|
||||
let data_len = data.length;
|
||||
let data_len = data_array.length;
|
||||
_send_to_websocket(this.http_handle, data_ptr, data_len, is_text);
|
||||
_free(data_ptr);
|
||||
}
|
||||
|
||||
close() {
|
||||
_close_websocket(this.http_handle);
|
||||
}
|
||||
|
||||
get readyState() {
|
||||
return this.status;
|
||||
}
|
||||
get bufferedAmount() {
|
||||
return 0;
|
||||
}
|
||||
get protocol() {
|
||||
return "";
|
||||
}
|
||||
get extensions() {
|
||||
return "";
|
||||
custom_close() {
|
||||
this.cleanup();
|
||||
this.status = this.CLOSED;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue