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
+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)
}