Skip to content

Commit f399abf

Browse files
committed
update FFI type again; handle port option; tag 0.0.1-a2
1 parent 7aa31a5 commit f399abf

File tree

5 files changed

+48
-41
lines changed

5 files changed

+48
-41
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "calcit_wss"
3-
version = "0.0.1-a1"
3+
version = "0.0.1-a2"
44
authors = ["jiyinyiyong <[email protected]>"]
55
edition = "2018"
66

calcit.cirru

Lines changed: 19 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compact.cirru

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
{} (:package |wss)
33
:configs $ {} (:init-fn |wss.test/main!) (:reload-fn |wss.test/reload!)
44
:modules $ []
5-
:version |0.0.3
5+
:version |0.0.1-a2
66
:files $ {}
77
|wss.core $ {}
88
:ns $ quote
99
ns wss.core $ :require
1010
wss.$meta :refer $ calcit-dirname
1111
wss.util :refer $ get-dylib-path
1212
:defs $ {}
13-
|wss-serve! $ quote
14-
defn wss-serve! (options cb)
15-
&call-dylib-edn-fn (get-dylib-path "\"/dylibs/libcalcit_wss") "\"wss_serve" options cb
1613
|wss-each! $ quote
1714
defn wss-each! (cb)
1815
&call-dylib-edn-fn (get-dylib-path "\"/dylibs/libcalcit_wss") "\"wss_each" cb
1916
|wss-send! $ quote
2017
defn wss-send! (client message)
2118
&call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_wss") "\"wss_send" client message
19+
|wss-serve! $ quote
20+
defn wss-serve! (options cb)
21+
&call-dylib-edn-fn (get-dylib-path "\"/dylibs/libcalcit_wss") "\"wss_serve" options cb
2222
|wss.test $ {}
2323
:ns $ quote
2424
ns wss.test $ :require

src/lib.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ lazy_static! {
1212
#[no_mangle]
1313
pub fn wss_serve(
1414
args: Vec<Edn>,
15-
handler: Arc<dyn Fn(Edn) -> Result<Edn, String> + Send + Sync + 'static>,
15+
handler: Arc<dyn Fn(Vec<Edn>) -> Result<Edn, String> + Send + Sync + 'static>,
1616
) -> Result<Edn, String> {
17-
println!("args: {:?}", args);
18-
// let result = handler(Edn::Str(msg.into_text().unwrap().to_string()));
17+
let port = match args.get(0) {
18+
Some(Edn::Map(m)) => match m.get(&Edn::Keyword(String::from("port"))) {
19+
Some(Edn::Number(n)) => n.floor().round() as u16,
20+
Some(a) => return Err(format!("Unknown port: {}", a)),
21+
None => 9001,
22+
},
23+
Some(Edn::Nil) => 9001,
24+
Some(a) => return Err(format!("Unknown option: {}", a)),
25+
None => 9001,
26+
};
1927

20-
// listen for WebSockets on port 9001:
21-
let event_hub = simple_websockets::launch(9001).expect("failed to listen on port 9001");
22-
// map between client ids and the client's `Responder`:
28+
// listen for WebSockets on port, defaults to 9001:
29+
let event_hub = simple_websockets::launch(port).expect("failed to listen on port 9001");
2330

2431
spawn(move || {
2532
loop {
@@ -35,10 +42,10 @@ pub fn wss_serve(
3542
let mut clients = CLIENTS.write().unwrap();
3643
clients.insert(client_id, responder);
3744
}
38-
if let Err(e) = handler(Edn::List(vec![
45+
if let Err(e) = handler(vec![Edn::List(vec![
3946
Edn::Keyword(String::from("connect")),
4047
Edn::Number(client_id as f64),
41-
])) {
48+
])]) {
4249
println!("Failed to handle connect: {}", e)
4350
}
4451
}
@@ -48,29 +55,29 @@ pub fn wss_serve(
4855
let mut clients = CLIENTS.write().unwrap();
4956
clients.remove(&client_id);
5057
}
51-
if let Err(e) = handler(Edn::List(vec![
58+
if let Err(e) = handler(vec![Edn::List(vec![
5259
Edn::Keyword(String::from("disconnect")),
5360
Edn::Number(client_id as f64),
54-
])) {
61+
])]) {
5562
println!("Failed to handle disconnect: {}", e)
5663
}
5764
}
5865
Event::Message(client_id, message) => match message {
5966
Message::Text(s) => {
60-
if let Err(e) = handler(Edn::List(vec![
67+
if let Err(e) = handler(vec![Edn::List(vec![
6168
Edn::Keyword(String::from("message")),
6269
Edn::Number(client_id as f64),
6370
Edn::Str(s),
64-
])) {
71+
])]) {
6572
println!("Failed to handle text message: {}", e)
6673
}
6774
}
6875
Message::Binary(buf) => {
69-
if let Err(e) = handler(Edn::List(vec![
76+
if let Err(e) = handler(vec![Edn::List(vec![
7077
Edn::Keyword(String::from("message")),
7178
Edn::Number(client_id as f64),
7279
Edn::Buffer(buf),
73-
])) {
80+
])]) {
7481
println!("Failed to handle binary message: {}", e)
7582
}
7683
}
@@ -104,7 +111,7 @@ pub fn wss_send(args: Vec<Edn>) -> Result<Edn, String> {
104111
#[no_mangle]
105112
pub fn wss_each(
106113
_args: Vec<Edn>,
107-
handler: Arc<dyn Fn(Edn) -> Result<Edn, String> + Send + Sync + 'static>,
114+
handler: Arc<dyn Fn(Vec<Edn>) -> Result<Edn, String> + Send + Sync + 'static>,
108115
) -> Result<Edn, String> {
109116
let mut ids: Vec<u64> = vec![];
110117
{
@@ -117,7 +124,7 @@ pub fn wss_each(
117124
}
118125

119126
for id in ids {
120-
handler(Edn::Number(id as f64))?;
127+
handler(vec![Edn::Number(id as f64)])?;
121128
}
122129

123130
Ok(Edn::Nil)

0 commit comments

Comments
 (0)