Move HELO to actually come from the server

This commit is contained in:
Tangent Wantwight 2020-05-23 14:24:49 -04:00
parent 3ac90e59b0
commit cf513ace26
3 changed files with 34 additions and 11 deletions

View file

@ -2,9 +2,10 @@
extern crate log;
use anyhow::{Context, Error, Result};
use future::ok;
use futures::prelude::*;
use net::{ClientMessage, ServerMessage};
use serde_json::{from_str, Value};
use net::{ClientMessage, Meta, ServerMessage};
use serde_json::{from_str, json, to_string, Value};
use std::net::ToSocketAddrs;
use stream::{iter, FuturesUnordered};
use structopt::StructOpt;
@ -46,7 +47,31 @@ async fn main() -> Result<()> {
}
async fn handle_socket(websocket: WebSocket) {
let (_sink, mut source) = websocket.split();
let (sink, mut source) = websocket.split();
let mut sink = sink.with(|msg: ServerMessage<Vec<Value>, Value>| {
let json = to_string(&msg).expect("JSON encoding shouldn't fail");
ok::<Message, Error>(Message::text(json))
});
let mut greeting = iter(vec![
ServerMessage::Meta {
m: Meta {
version: "Unstable",
helo: Some("Dedicated base2020 server".into()),
},
},
ServerMessage::SetState {
u: 0,
s: json!({}),
},
])
.map(Ok);
if let Err(err) = sink.send_all(&mut greeting).await {
warn!("Websocket send error: {:#}", err);
return;
}
let error: Option<Error> = loop {
match source.next().await {

View file

@ -20,11 +20,6 @@ export class Connection<LocalInput, State> {
const ws = new WebSocket(this.url);
const source: Source<ServerMessage<LocalInput[], State>> = create((data, { }, close) => {
ws.onopen = () => {
// fake a HELO message & set state message until the server actually sends them
data({ t: MessageTypes.META, helo: "Websocket Server" });
data({ t: MessageTypes.SET_STATE, u: 0, s: {} });
};
ws.onmessage = msg => {
const decoded: Jsonified<ServerMessage<LocalInput[], State>> = JSON.parse(msg.data);
data(decoded as ServerMessage<LocalInput[], State>);

View file

@ -13,15 +13,18 @@ pub enum ClientMessage<I, S> {
#[derive(Serialize, Debug)]
pub struct Meta {
helo: Option<String>,
version: &'static str,
pub helo: Option<String>,
pub version: &'static str,
}
#[derive(Serialize, Debug)]
#[serde(tag = "t")]
pub enum ServerMessage<I, S> {
#[serde(rename = "m")]
Meta { m: Meta },
Meta {
#[serde(flatten)]
m: Meta,
},
#[serde(rename = "s")]
SetState { u: i8, s: S },
#[serde(rename = "i")]