state, fixes
This commit is contained in:
parent
d013ffebbb
commit
7bf1e76329
4 changed files with 59 additions and 18 deletions
|
@ -21,8 +21,8 @@ let ws = {};
|
||||||
let i = 0;
|
let i = 0;
|
||||||
|
|
||||||
const WS_NOT_EXISTING = -1;
|
const WS_NOT_EXISTING = -1;
|
||||||
const WS_CREATING = 0;
|
const WS_DISCONNECTED = 0;
|
||||||
const WS_OPEN = 1;
|
const WS_CONNECTED = 1;
|
||||||
const WS_CLOSED = 2;
|
const WS_CLOSED = 2;
|
||||||
|
|
||||||
function ws_open(ptr, len) {
|
function ws_open(ptr, len) {
|
||||||
|
@ -32,15 +32,15 @@ function ws_open(ptr, len) {
|
||||||
try {
|
try {
|
||||||
conn = {
|
conn = {
|
||||||
socket: new WebSocket(url),
|
socket: new WebSocket(url),
|
||||||
state: 0,
|
state: WS_DISCONNECTED,
|
||||||
received: []
|
received: []
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
conn.socket.onopen = (s, e) => {
|
conn.socket.onopen = (s, e) => {
|
||||||
conn.state = WS_OPEN;
|
conn.state = WS_CONNECTED;
|
||||||
};
|
};
|
||||||
conn.socket.onmessage = (s, e) => {
|
conn.socket.onmessage = (s, e) => {
|
||||||
conn.received.push(new Uint8Array(e.data.arraybuffer()));
|
conn.received.push(new Uint8Array(e.data.arraybuffer()));
|
||||||
|
@ -63,7 +63,7 @@ function ws_write(id, ptr, len) {
|
||||||
for (let i = 0; i < len; i++) {
|
for (let i = 0; i < len; i++) {
|
||||||
data[i] = data_in[i];
|
data[i] = data_in[i];
|
||||||
}*/
|
}*/
|
||||||
if (ws[id] != null) {
|
if (ws[id] != null && ws[id].socket.readyState == WebSocket.OPEN) {
|
||||||
ws[id].socket.send(data);
|
ws[id].socket.send(data);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
22
src/lib.rs
22
src/lib.rs
|
@ -8,6 +8,13 @@ pub mod pc;
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
use pc::*;
|
use pc::*;
|
||||||
|
|
||||||
|
#[repr(i32)]
|
||||||
|
pub enum QuadWsState {
|
||||||
|
WsNotExisting = -1,
|
||||||
|
WsDisconnected = 0,
|
||||||
|
WsConnected = 1,
|
||||||
|
WsClosed = 2,
|
||||||
|
}
|
||||||
pub struct QuadWs {
|
pub struct QuadWs {
|
||||||
channel: WsChannnel,
|
channel: WsChannnel,
|
||||||
}
|
}
|
||||||
|
@ -23,7 +30,20 @@ impl QuadWs {
|
||||||
pub fn close(&mut self) {
|
pub fn close(&mut self) {
|
||||||
ws_close_rust(&mut self.channel)
|
ws_close_rust(&mut self.channel)
|
||||||
}
|
}
|
||||||
pub fn read(&mut self) -> Option<Vec<u8>>{
|
pub fn read(&mut self) -> Option<Vec<u8>> {
|
||||||
ws_read_rust(&mut self.channel)
|
ws_read_rust(&mut self.channel)
|
||||||
}
|
}
|
||||||
|
pub fn connected(&mut self) -> bool {
|
||||||
|
if let QuadWsState::WsConnected = self.state(){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
pub fn state(&mut self) -> QuadWsState {
|
||||||
|
let state = ws_state_rust(&mut self.channel);
|
||||||
|
if state < 0 || state > 2 {
|
||||||
|
return QuadWsState::WsClosed;
|
||||||
|
}
|
||||||
|
unsafe { ::std::mem::transmute(state) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
18
src/pc.rs
18
src/pc.rs
|
@ -103,14 +103,30 @@ pub fn ws_read_rust(socket: &mut WsChannnel) -> Option<Vec<u8>> {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ws_close_rust(socket: &WsChannnel) {
|
pub fn ws_close_rust(socket: &mut WsChannnel) {
|
||||||
match &socket.client {
|
match &socket.client {
|
||||||
WsClient::None => {}
|
WsClient::None => {}
|
||||||
WsClient::Insecure(client) => {
|
WsClient::Insecure(client) => {
|
||||||
client.shutdown().unwrap();
|
client.shutdown().unwrap();
|
||||||
|
socket.client = WsClient::None;
|
||||||
}
|
}
|
||||||
WsClient::Secure(client) => {
|
WsClient::Secure(client) => {
|
||||||
client.shutdown().unwrap();
|
client.shutdown().unwrap();
|
||||||
|
socket.client = WsClient::None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn ws_state_rust(socket: &mut WsChannnel) -> i32{
|
||||||
|
match &socket.client {
|
||||||
|
WsClient::None => {
|
||||||
|
2
|
||||||
|
}
|
||||||
|
WsClient::Insecure(_) => {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
WsClient::Secure(_) => {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
25
src/wasm.rs
25
src/wasm.rs
|
@ -11,32 +11,37 @@ extern "C" {
|
||||||
fn ws_close(id: WsChannnel);
|
fn ws_close(id: WsChannnel);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ws_open_rust(url: String) -> Result<WsChannnel, _> {
|
pub fn ws_open_rust(url: String) -> Option<WsChannnel> {
|
||||||
let url = CString::new(url).unwrap();
|
let url = CString::new(url).unwrap();
|
||||||
let socket_id = unsafe { ws_open(url.as_ptr(), url.as_bytes().len() as u32) };
|
let socket_id = unsafe { ws_open(url.as_ptr(), url.as_bytes().len() as u32) };
|
||||||
if socket_id < 0 {
|
if socket_id < 0 {
|
||||||
Err(())
|
None
|
||||||
} else {
|
} else {
|
||||||
Ok(socket_id)
|
Some(socket_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ws_write_rust(socket: WsChannnel, data: Vec<u8>) -> bool {
|
pub fn ws_write_rust(socket: &mut WsChannnel, data: Vec<u8>) -> bool {
|
||||||
let buf = data.as_slice();
|
let buf = data.as_slice();
|
||||||
let succ = unsafe { ws_write(socket, buf.as_ptr(), buf.len() as u32) };
|
let succ = unsafe { ws_write(*socket, buf.as_ptr(), buf.len() as u32) };
|
||||||
succ
|
succ
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ws_read_rust(socket: WsChannnel) -> Option<Vec<u8>> {
|
pub fn ws_read_rust(socket: &mut WsChannnel) -> Option<Vec<u8>> {
|
||||||
let available = unsafe { ws_available(socket) };
|
let available = unsafe { ws_available(*socket) };
|
||||||
if available < 0 {
|
if available < 0 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let mut buffer = vec![0; available as usize];
|
let mut buffer = vec![0; available as usize];
|
||||||
unsafe { ws_read(socket, buffer.as_mut_ptr(), available as u32) };
|
unsafe { ws_read(*socket, buffer.as_mut_ptr(), available as u32) };
|
||||||
return Some(buffer);
|
return Some(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn ws_close_rust(socket: WsChannnel) {
|
pub fn ws_close_rust(socket: &mut WsChannnel) {
|
||||||
unsafe { ws_close(socket) };
|
unsafe { ws_close(*socket) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn ws_state_rust(socket: &mut WsChannnel) -> i32{
|
||||||
|
let state = unsafe { ws_state(*socket) };
|
||||||
|
return state;
|
||||||
|
}
|
Loading…
Reference in a new issue