-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
204 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target | ||
.vscode | ||
.vscode | ||
.idea |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/// | ||
/// This example demonstrates how to use websockets. | ||
/// | ||
use rustyscript::{Error, json_args, Module, Runtime, RuntimeOptions, Undefined}; | ||
|
||
fn main() -> Result<(), Error> { | ||
let module = Module::new( | ||
"test.js", | ||
" | ||
export async function connect(url) { | ||
return new Promise((resolve, reject) => { | ||
const ws = new WebSocket(url); | ||
ws.addEventListener('open', () => { | ||
console.log(ws.readyState); | ||
ws.send('ping'); | ||
}); | ||
ws.addEventListener('message', (event) => { | ||
console.log(event.data); | ||
ws.close(); | ||
}); | ||
ws.addEventListener('close', (event) => { | ||
if (event.wasClean) { | ||
console.log(`Connection closed, code=${event.code} reason=${event.reason}`); | ||
resolve(`Connection closed, code=${event.code} reason=${event.reason}`); | ||
} else { | ||
console.log('Connection died'); | ||
reject(new Error('Connection died')); | ||
} | ||
}); | ||
ws.addEventListener('error', (e) => { | ||
console.log(`Error: ${e}`); | ||
reject(new Error(`Error: ${e}`)); | ||
}); | ||
}); | ||
} | ||
", | ||
); | ||
|
||
let mut runtime = Runtime::new(RuntimeOptions { | ||
default_entrypoint: Some("connect".to_string()), | ||
..Default::default() | ||
})?; | ||
|
||
let module_handle = runtime.load_module(&module)?; | ||
|
||
runtime.call_entrypoint::<Undefined>(&module_handle, json_args!("wss://echo.websocket.org"))?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import * as websocket from "ext:deno_websocket/01_websocket.js"; | ||
import * as websocketStream from "ext:deno_websocket/02_websocketstream.js"; | ||
|
||
import { applyToGlobal, getterOnly, nonEnumerable } from 'ext:rustyscript/rustyscript.js'; | ||
|
||
applyToGlobal({ | ||
WebSocket: nonEnumerable(websocket.WebSocket), | ||
CloseEvent: nonEnumerable(websocket.CloseEvent) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use deno_core::{extension, Extension}; | ||
use deno_core::error::AnyError; | ||
use deno_core::url::Url; | ||
use deno_websocket::WebSocketPermissions; | ||
use crate::ext::web::Permissions; | ||
|
||
extension!( | ||
init_websocket, | ||
deps = [rustyscript], | ||
esm_entry_point = "ext:init_websocket/init_websocket.js", | ||
esm = [ dir "src/ext/websocket", "init_websocket.js" ], | ||
); | ||
|
||
impl WebSocketPermissions for Permissions { | ||
fn check_net_url(&mut self, _url: &Url, _api_name: &str) -> Result<(), AnyError> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn extensions(user_agent: String) -> Vec<Extension> { | ||
vec![ | ||
deno_websocket::deno_websocket::init_ops_and_esm::<Permissions>(user_agent, None, None), | ||
init_websocket::init_ops_and_esm(), | ||
] | ||
} | ||
|
||
pub fn snapshot_extensions(user_agent: String) -> Vec<Extension> { | ||
vec![ | ||
deno_websocket::deno_websocket::init_ops::<Permissions>(user_agent, None, None), | ||
init_websocket::init_ops(), | ||
] | ||
} |