Skip to content

Instantly share code, notes, and snippets.

@BillBarnhill
Last active March 16, 2021 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BillBarnhill/f8e9fd330cc0011d09b97399d27a2ee9 to your computer and use it in GitHub Desktop.
Save BillBarnhill/f8e9fd330cc0011d09b97399d27a2ee9 to your computer and use it in GitHub Desktop.
A main.rs for tide-websocket use.Unfortunately it closes the WebSocket immediately
// This is at present within a script tag in html page
app = function(){
function onLoad()
{
console.log("[page] loaded");
this.websocket = new WebSocket(this.wsUri);
this.websocket.onopen = this.onWsOpen;
this.websocket.onclose = this.onWsClose;
this.websocket.onmessage = this.onWsMessage;
this.websocket.onerror = this.onWsError;
}
function onWsOpen(ev)
{
console.log(`[ws open] ${this.websocket}`);
this.websocket.send("Testing 123");
}
function onWsClose(ev)
{
if (ev.wasClean) {
console.log(`[ws close] Connection closed cleanly, code=${ev.code} reason=${ev.reason}`);
} else {
console.log(`[ws close] Connection died, ev=${ev}`);
}
}
function onWsMessage(ev)
{
console.log(`[ws rcv] ${ev.data}`);
}
function onWsError(ev)
{
console.log(`[ws err] ${ev}`);
}
function allowDrop(ev)
{
ev.preventDefault();
}
function drag(ev)
{
var srcId=ev.target.id;
console.log("drag=>target:"+srcId);
ev.dataTransfer.setData("text", srcId);
}
function drop(ev)
{
ev.preventDefault();
var srcId=ev.dataTransfer.getData("text");
var text = document.getElementById(srcId).textContent;
console.log("drop=>srcId:"+srcId+" text:"+text);
event.target.textContent = text;
document.getElementById("choice").value = text;
this.chosen = document.getElementById("choice").value;
}
function is_locked()
{
return (locked == true)
}
function lock()
{
console.log("Now locked at "+this.chosen);
locked = true
}
function card()
{
return this.chosen;
}
return {
onLoad: onLoad,
allowDrop: allowDrop,
drag: drag,
drop: drop,
lock: lock,
card: card,
onWsOpen: onWsOpen,
onWsClose: onWsClose,
onWsMessage: onWsMessage,
onWsError: onWsError,
chosen: "?",
locked: false,
wsUri: "ws://127.0.0.1:8080/ws",
websocket: undefined
}
}();
use serde_json::{Map, Value};
use tide_websockets::{
WebSocket
};
use async_std::stream::StreamExt;
const _DEFAULT_4XX_BODY: &[u8] = b"Oops! I can't find what you're looking for..." as &[_];
const _DEFAULT_5XX_BODY: &[u8] = b"I'm broken, apparently." as &[_];
fn start_msg() -> Map<String, Value> {
let mut map = Map::new();
map.insert("Bool Example".to_string(), false.into());
map.insert("String Example".to_string(), "Str".to_string().into());
map.insert("Number Example".to_string(), 2.into());
map
}
#[async_std::main]
async fn main() -> std::result::Result<(), std::io::Error> {
tide::log::start();
let mut app = tide::new();
app.at("/").serve_file("plan.html")?;
app.at("/ws").get(WebSocket::new(
|_req, mut stream| async move {
stream.send_json(&start_msg()).await.unwrap();
loop {
let msg = stream.next().await.unwrap();
tide::log::info!("Received msg-> {:?}\n", msg);
}
}));
app.listen("127.0.0.1:8080").await?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment