Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@
name = "nakama-rs"
version = "0.1.1"
authors = ["Fedor Logachev <not.fl3@gmail.com>"]
edition = "2018"
edition = "2021"
license = "MIT/Apache-2.0"
description = "Pure rust nakama protocol implementation"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
test = []

[dependencies]
base64 = "0.13"
nanoserde = "0.1.26"
base64 = "0.21.2"
nanoserde = "0.1.32"
urlencoding = "2.0.0-alpha.1"
async-trait = "0.1.50"
oneshot = "0.1.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this dependency removed?

log = "0.4.14"
isahc = "1.4.0"
qws = { version = "0.7.9", features = ["nativetls"] }
chrono = "0.4.19"

#[cfg(feature = "test")]
async-trait = "0.1.71"
log = "0.4.19"
isahc = "1.7.2"
ws = { version = "0.9.2" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this introduced instead of qws?

chrono = { version = "0.4.26", features = ["serde"] }
tokio = { version = "1", features = ["full"] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this introduced?

serde = { version = "1", features = ["derive", "rc"] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this introduced?

parking_lot = { version = "0.12.1", features = ["serde", "arc_lock"] }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this introduced?

futures = "0.3.15"
hyper = "=0.13.10"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this introduced?

serde_json = "1.0.100"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this introduced?



[dev-dependencies]
simple_logger = "1.11.0"
cassette = "0.2.3"
anyhow = "1.0.71"
8 changes: 6 additions & 2 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ fn main() {
.await;
let session = session.unwrap();
let session2 = session2.unwrap();
web_socket.connect(&session, true, -1).await;
web_socket2.connect(&session2, true, -1).await;
web_socket
.connect("ws://127.0.0.1:7350", &session, true, -1)
.await;
web_socket2
.connect("ws://127.0.0.1:7350", &session2, true, -1)
.await;
}
});

Expand Down
8 changes: 6 additions & 2 deletions examples/async_single_threaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ fn main() {
.await;
let session = session.unwrap();
let session2 = session2.unwrap();
web_socket.connect(&session, true, -1).await;
web_socket2.connect(&session2, true, -1).await;
web_socket
Copy link
Contributor

@lugehorsam lugehorsam Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this accept a host/port string now? There shouldn't be cases where the socket connects to a host/port that is different from the client.

.connect("ws://127.0.0.1:7350", &session, true, -1)
.await;
web_socket2
.connect("ws://127.0.0.1:7350", &session2, true, -1)
.await;
state.replace(Connected);
}
JoiningChat => {
Expand Down
75 changes: 75 additions & 0 deletions examples/create_leaderboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2021 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use chrono::Utc;
use std::collections::HashMap;
use std::ops::Deref;
use std::sync::Arc;
use std::thread::{sleep, spawn};
use std::time::Duration;

use futures::executor::block_on;
use simple_logger::SimpleLogger;

use log::{trace, LevelFilter};
use nakama_rs::api::ApiOverrideOperator;
use nakama_rs::client::Client;
use nakama_rs::default_client::DefaultClient;
use nakama_rs::http_adapter::RestHttpAdapter;
use nakama_rs::matchmaker::Matchmaker;
use nakama_rs::socket::{MatchmakerMatched, Socket};
use nakama_rs::web_socket::WebSocket;
use nakama_rs::web_socket_adapter::WebSocketAdapter;
use nakama_rs::*;
use tokio::sync::mpsc::channel;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
SimpleLogger::new()
.with_level(LevelFilter::Off)
.with_module_level("nakama_rs", LevelFilter::Trace)
.init()
.unwrap();

let http_adapter = RestHttpAdapter::new("http://127.0.0.1", 7350);
let client = DefaultClient::new(http_adapter, "defaultkey", "");
let session = client
.authenticate_custom("00000000", None, true, Default::default())
.await?;
println!("{:#?}", session);
let data = client
.create_leaderboard(&session, ApiOverrideOperator::SET)
.await?;
println!("{:?}", data);
Ok(())
}

// curl 'http://127.0.0.1:7351/v2/console/api/endpoints/rpc/clientrpc.create_leaderboard' \
// -H 'Accept: application/json, text/plain, */*' \
// -H 'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7' \
// -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJkYzUwZjcyMy1lZjZmLTQ1N2ItOTM0MS0zMGM0YWI1NDcwMjUiLCJ1c24iOiJ6YXR5Vk5VdWlBIiwiZXhwIjoxNjkwNDI3NDU4fQ.G3VjQtCB4e2_vKP9i28kupcPHdgzPBcNQU6D65kutjE' \
// -H 'Connection: keep-alive' \
// -H 'Content-Type: application/json' \
// -H 'Cookie: ajs_anonymous_id=a9b3295b-da65-4d84-9bab-d8c4acef6341' \
// -H 'Origin: http://127.0.0.1:7351' \
// -H 'Referer: http://127.0.0.1:7351/' \
// -H 'Sec-Fetch-Dest: empty' \
// -H 'Sec-Fetch-Mode: cors' \
// -H 'Sec-Fetch-Site: same-origin' \
// -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36' \
// -H 'sec-ch-ua: "Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"' \
// -H 'sec-ch-ua-mobile: ?0' \
// -H 'sec-ch-ua-platform: "macOS"' \
// --data-raw '{"user_id":"","body":"{\"operator\":\"set\"}"}' \
// --compressed
96 changes: 96 additions & 0 deletions examples/join_party.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2021 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use chrono::Utc;
use std::collections::HashMap;
use std::ops::Deref;
use std::sync::Arc;
use std::thread::{sleep, spawn};
use std::time::Duration;

use futures::executor::block_on;
use simple_logger::SimpleLogger;

use log::{trace, LevelFilter};
use nakama_rs::client::Client;
use nakama_rs::default_client::DefaultClient;
use nakama_rs::http_adapter::RestHttpAdapter;
use nakama_rs::matchmaker::Matchmaker;
use nakama_rs::socket::{MatchmakerMatched, Socket};
use nakama_rs::web_socket::WebSocket;
use nakama_rs::web_socket_adapter::WebSocketAdapter;
use nakama_rs::*;
use tokio::sync::mpsc::channel;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
SimpleLogger::new()
.with_level(LevelFilter::Off)
.with_module_level("nakama_rs", LevelFilter::Trace)
.init()
.unwrap();

let http_adapter = RestHttpAdapter::new("http://127.0.0.1", 7350);
let client = DefaultClient::new(http_adapter, "defaultkey", "");
let adapter = WebSocketAdapter::new();
let mut web_socket = WebSocket::new(adapter);
let web1 = web_socket.clone();
let (mut kill_tick, mut rc_kill) = channel(1);
let res = tokio::spawn(async move {
loop {
// This could also be called in a different thread than the main/game thread. The callbacks
// will be called in the same thread, invoking e.g. `on_received_channel_message`.
// Note that `tick` is also necessary to wake futures like `web_socket.join_chat` - it is not only necessary
// for the callbacks.
trace!("Ticking websockets");
web1.tick();
if let Ok(v) = rc_kill.try_recv() {
return v;
}

sleep(Duration::from_millis(500));
}
});
let session = client
.authenticate_custom(
&Utc::now().timestamp_nanos().to_string(),
None,
true,
HashMap::new(),
)
.await;

let session = session.unwrap();
web_socket
.connect("ws://127.0.0.1:7350", &session, true, -1)
.await;

let party = web_socket
.join_party("be9cad6f-96a0-4cf7-8ad5-c3ff91ff4c23.nakama")
.await?;
// let party = web_socket.create_party(true, 2).await?;
println!("********{:?}", party);
// kill_tick.send(1).await;

// web_socket.on_received_party_join_request(move |x| {
// block_on(kill_tick.send(x));
// });
// kill_tick.send(1).await;
let data = res.await?;
// web_socket.remove_matchmaker(&ticked.ticket).await;
Ok(())
}

// "ws://127.0.0.1:7350/ws?lang=en&status=true&token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJmOTE5MGFlYy03NGI2LTQ0MDktOTBkYy1hNDBiNGRhZGQzMmYiLCJ1c24iOiJVYm1qd2tMWGpEIiwiZXhwIjoxNjg4OTc4MzQxfQ.-zNwQvuIcu8KphjckTmWg6d5aPMVcXsQV5KHMODYAH0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the commented out code here?

// "ws://127.0.0.1:7350/ws?lang=en&status=true&token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJmOTE5MGFlYy03NGI2LTQ0MDktOTBkYy1hNDBiNGRhZGQzMmYiLCJ1c24iOiJVYm1qd2tMWGpEIiwiZXhwIjoxNjg4OTc4NDI5fQ.FIj8tu2b1pTaTjhSrkCUBje0Quv7QLkDqur4M7fa7JM"
106 changes: 106 additions & 0 deletions examples/matchmaker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2021 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use chrono::Utc;
use std::collections::HashMap;
use std::ops::Deref;
use std::sync::Arc;
use std::thread::{sleep, spawn};
use std::time::Duration;

use futures::executor::block_on;
use simple_logger::SimpleLogger;

use log::{trace, LevelFilter};
use nakama_rs::client::Client;
use nakama_rs::default_client::DefaultClient;
use nakama_rs::http_adapter::RestHttpAdapter;
use nakama_rs::matchmaker::Matchmaker;
use nakama_rs::socket::{MatchmakerMatched, Socket};
use nakama_rs::web_socket::WebSocket;
use nakama_rs::web_socket_adapter::WebSocketAdapter;
use nakama_rs::*;
use tokio::sync::mpsc::channel;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
SimpleLogger::new()
.with_level(LevelFilter::Off)
.with_module_level("nakama_rs", LevelFilter::Trace)
.init()
.unwrap();

let http_adapter = RestHttpAdapter::new("http://127.0.0.1", 7350);
let client = DefaultClient::new(http_adapter, "defaultkey", "");
let adapter = WebSocketAdapter::new();
let mut web_socket = WebSocket::new(adapter);
let web1 = web_socket.clone();
let (mut kill_tick, mut rc_kill) = channel(1);
let res = tokio::spawn(async move {
loop {
// This could also be called in a different thread than the main/game thread. The callbacks
// will be called in the same thread, invoking e.g. `on_received_channel_message`.
// Note that `tick` is also necessary to wake futures like `web_socket.join_chat` - it is not only necessary
// for the callbacks.
println!("Ticking websockets");
web1.tick();
if let Ok(v) = rc_kill.try_recv() {
return v;
}

sleep(Duration::from_millis(500));
}
});
let session = client
.authenticate_custom(
&Utc::now().timestamp_nanos().to_string(),
None,
true,
HashMap::new(),
)
.await;

let session = session.unwrap();
web_socket
.connect("ws://127.0.0.1:7350", &session, true, -1)
.await;
let mut numeric_properties = HashMap::new();
numeric_properties.insert("mmr".to_string(), 1500.0);
numeric_properties.insert("room_id".to_string(), 30.0);
let mut string_properties = HashMap::new();
string_properties.insert("battle_type".to_string(), "QuickMatch1v1".to_string());
let ticked = web_socket
.add_matchmaker(&Matchmaker {
min_count: 2,
max_count: 2,
string_properties,
numeric_properties,
query: "+properties.battle_type:QuickMatch1v1 -properties.room_id:30 properties.mmr:>=1400 properties.mmr:<=1600".to_string(),
})
.await
.expect("Failed to join chat");
// let party = web_socket.create_party(true, 2).await?;
// println!("********{:?}", party);
// kill_tick.send(1).await;
web_socket.on_received_matchmaker_matched(move |x| {
block_on(kill_tick.send(x));
});
let data = res.await?;
println!("********{:#?}", data);
// web_socket.remove_matchmaker(&ticked.ticket).await;
Ok(())
}

// "ws://127.0.0.1:7350/ws?lang=en&status=true&token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJmOTE5MGFlYy03NGI2LTQ0MDktOTBkYy1hNDBiNGRhZGQzMmYiLCJ1c24iOiJVYm1qd2tMWGpEIiwiZXhwIjoxNjg4OTc4MzQxfQ.-zNwQvuIcu8KphjckTmWg6d5aPMVcXsQV5KHMODYAH0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the commented out code here?

// "ws://127.0.0.1:7350/ws?lang=en&status=true&token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJmOTE5MGFlYy03NGI2LTQ0MDktOTBkYy1hNDBiNGRhZGQzMmYiLCJ1c24iOiJVYm1qd2tMWGpEIiwiZXhwIjoxNjg4OTc4NDI5fQ.FIj8tu2b1pTaTjhSrkCUBje0Quv7QLkDqur4M7fa7JM"
Loading