housleyjk / ws-rs

Lightweight, event-driven WebSockets for Rust.
MIT License
1.46k stars 221 forks source link

Examples in guide do not work #303

Closed Jasperav closed 4 years ago

Jasperav commented 4 years ago

I created a plain new project and followed the guide here: https://ws-rs.org/guide

This lines of code:

// A WebSocket echo server

extern crate ws;

use ws::listen;

fn main() {
  listen("127.0.0.1:3012", |out| {
      move |msg| {
         out.send(msg)
      }
  }).unwrap()
} 

Will throw an error after I visit the url in my browser with the error:

Encountered an error: WebSocket Protocol Error: Unable to parse WebSocket key. Enable a logger to see more information.

Same error pops up in my browser.

This example:

// A WebSocket client that sends one message then closes
extern crate ws;

use ws::{connect, CloseCode};

fn main() {
  connect("ws://127.0.0.1:3012", |out| {
      out.send("Hello WebSocket").unwrap();

      move |msg| {
          println!("Got message: {}", msg);
          out.close(CloseCode::Normal)
      }
  }).unwrap()
} 

instantly throws this error in my debugger:

Encountered an error: broken pipe Enable a logger to see more information.

How to fix this?

Jasperav commented 4 years ago

Nvm, got it working

benmarten commented 4 years ago

Nvm, got it working

Ideally, you share how you got it working?

benmarten commented 4 years ago

For me it was, that i was using socket.io, instead of the native client websocket implementation: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket Now it works ;)