I've this simple html file, the is saved in the root directory, i.e. beside the Cargo.toml
file:
index.html
<!DOCTYPE html>
<html>
<head>
<title>Working with Openlayers</title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
<!-- Openlayers CSS file-->
<style type="text/css">
#map{
width:100%;
height:600px;
}
</style>
<!--Basic styling for map div,
if height is not defined the div will show up with 0 px height -->
</head>
<body>
<h1>Map</h1>
<div id="map">
<!-- Your map will be shown inside this div-->
</div>
</body>
<script src="https://openlayers.org/en/v4.6.5/build/ol.js" type="text/javascript"></script>
<!-- Openlayesr JS fIle -->
<script type="text/javascript" src="map.js" type="text/javascript"></script>
<!-- Our map file -->
</html>
And running the server using the ws-rs
crate, as below:
fn on_request(&mut self, req: &Request) -> Result<(Response)> {
// Using multiple handlers is better (see router example)
match req.resource() {
// The default trait implementation
"/ws" => Response::from_request(req),
// Create a custom response
"/" => {
let filename = "index.html";
let contents = fs::read_to_string(filename).expect("Something went wrong reading the file");
Ok(Response::new(200, "OK", contents.as_bytes().to_vec()))
}
_ => Ok(Response::new(404, "Not Found", b"404 - Not Found".to_vec())),
}
}
Where shall i save my map.js
file, as I'm getting an error"
GET http://127.0.0.1:8080/map.js 404 (Not Found)
Full code is below for the main.rs
is below:
use ws::{Handler, Message, Request, Response, Result, Sender, CloseCode, WebSocket};
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
use std::fs;
struct Server {
out: Sender,
}
#[derive(Serialize, Deserialize, Debug)]
struct Location {
unique_id: String,
lat: f64,
lon: f64,
speed: f64
}
impl Handler for Server {
fn on_message(&mut self, msg: Message) -> Result<()> {
println!("Server got message '{}'. ", msg);
let x = msg.as_text().unwrap();
let z = "Hello from Google Android SDK built for x86_64";
/* if x != z {
let deserialized: Location = serde_json::from_str(x).unwrap();
println!("Deserialized: {:?}", deserialized);
println!("lat = {}, long = {}, speed = {}",
deserialized.lat, deserialized.lon, deserialized.speed);
}
Ok(())
*/
// self.out.send(msg)
self.out.broadcast(msg)
}
fn on_close(&mut self, code: CloseCode, reason: &str) {
match code {
CloseCode::Normal => println!("The client is done with the connection."),
CloseCode::Away => println!("The client is leaving the site."),
_ => println!("The client encountered an error: {}", reason),
}
}
fn on_request(&mut self, req: &Request) -> Result<(Response)> {
// Using multiple handlers is better (see router example)
match req.resource() {
// The default trait implementation
"/ws" => Response::from_request(req),
// Create a custom response
"/" => {
let filename = "index.html";
let contents = fs::read_to_string(filename).expect("Something went wrong reading the file");
Ok(Response::new(200, "OK", contents.as_bytes().to_vec()))
}
_ => Ok(Response::new(404, "Not Found", b"404 - Not Found".to_vec())),
}
}
}
fn main() {
let my_addr = "127.0.0.1:8080";
let mut me = ws::WebSocket::new(|out| Server { out }).unwrap();
me.listen(my_addr).unwrap();
}