handle cross socket message passing

This commit is contained in:
2025-05-18 16:32:01 -06:00
parent b6a4936247
commit af0fd811b3
10 changed files with 240 additions and 63 deletions
+1
View File
@@ -15,6 +15,7 @@ version = "1.0.0"
[dependencies]
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
gleam_json = ">= 2.3.0 and < 3.0.0"
gleam_crypto = ">= 1.5.0 and < 2.0.0"
[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
+2
View File
@@ -2,12 +2,14 @@
# You typically do not need to edit this file
packages = [
{ name = "gleam_crypto", version = "1.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "917BC8B87DBD584830E3B389CBCAB140FFE7CB27866D27C6D0FB87A9ECF35602" },
{ name = "gleam_json", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C55C5C2B318533A8072D221C5E06E5A75711C129E420DD1CE463342106012E5D" },
{ name = "gleam_stdlib", version = "0.59.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "F8FEE9B35797301994B81AF75508CF87C328FE1585558B0FFD188DC2B32EAA95" },
{ name = "gleeunit", version = "1.3.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "A7DD6C07B7DA49A6E28796058AA89E651D233B357D5607006D70619CD89DAAAB" },
]
[requirements]
gleam_crypto = { version = ">= 1.5.0 and < 2.0.0" }
gleam_json = { version = ">= 2.3.0 and < 3.0.0" }
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
+14 -2
View File
@@ -1,8 +1,10 @@
import gleam/bit_array
import gleam/crypto
import gleam/dynamic/decode
import gleam/json
pub type PlayerSession {
PlayerSession(id: Int, token_hash: String, username: String)
PlayerSession(id: Int, token_hash: String, username: String, socket_id: Int)
}
pub fn encode_player_session(player_session: PlayerSession) -> json.Json {
@@ -10,6 +12,7 @@ pub fn encode_player_session(player_session: PlayerSession) -> json.Json {
#("id", json.int(player_session.id)),
#("token_hash", json.string(player_session.token_hash)),
#("username", json.string(player_session.username)),
#("socket_id", json.int(player_session.socket_id)),
])
}
@@ -17,5 +20,14 @@ pub fn player_session_decoder() -> decode.Decoder(PlayerSession) {
use id <- decode.field("id", decode.int)
use token_hash <- decode.field("token_hash", decode.string)
use username <- decode.field("username", decode.string)
decode.success(PlayerSession(id:, token_hash:, username:))
use socket_id <- decode.field("socket_id", decode.int)
decode.success(PlayerSession(id:, token_hash:, username:, socket_id:))
}
pub fn hash_token(token: String) -> String {
let token =
crypto.new_hasher(crypto.Sha512)
|> crypto.hash_chunk(bit_array.from_string(token))
|> crypto.digest
|> bit_array.base64_encode(True)
}