add: broadcast socket id for user sockets
This commit is contained in:
parent
ecde5d3d46
commit
1724f798ca
6 changed files with 48 additions and 16 deletions
|
@ -52,10 +52,9 @@ async fn main() {
|
||||||
let html_path = write_assets(&config).await;
|
let html_path = write_assets(&config).await;
|
||||||
|
|
||||||
// governor
|
// governor
|
||||||
// (1000/125) * 32 = 256 requests/second
|
|
||||||
let governor_config = Arc::new(
|
let governor_config = Arc::new(
|
||||||
GovernorConfigBuilder::default()
|
GovernorConfigBuilder::default()
|
||||||
.per_millisecond(125)
|
.per_millisecond(75)
|
||||||
.burst_size(32)
|
.burst_size(32)
|
||||||
.finish()
|
.finish()
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
|
|
|
@ -100,7 +100,7 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% if user %}
|
{% if user %}
|
||||||
<div class="card flex gap-2" id="join_or_leave">
|
<div class="card flex gap-2 flex-wrap" id="join_or_leave">
|
||||||
{% if not is_owner %} {% if not is_joined %} {% if not
|
{% if not is_owner %} {% if not is_joined %} {% if not
|
||||||
is_pending %}
|
is_pending %}
|
||||||
<button class="primary" onclick="join_community()">
|
<button class="primary" onclick="join_community()">
|
||||||
|
@ -216,8 +216,15 @@
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
{% endif %} {% endif %} {% if can_manage_community or
|
{% endif %} {% else %}
|
||||||
is_manager %}
|
<a
|
||||||
|
href="/chats/{{ community.id }}/0"
|
||||||
|
class="button quaternary"
|
||||||
|
>
|
||||||
|
{{ icon "message-circle" }}
|
||||||
|
<span>{{ text "communities:label.chats" }}</span>
|
||||||
|
</a>
|
||||||
|
{% endif %} {% if can_manage_community or is_manager %}
|
||||||
<a
|
<a
|
||||||
href="/community/{{ community.id }}/manage"
|
href="/community/{{ community.id }}/manage"
|
||||||
class="button primary"
|
class="button primary"
|
||||||
|
@ -229,6 +236,7 @@
|
||||||
</a>
|
</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
@ -240,18 +240,13 @@
|
||||||
|
|
||||||
streams.subscribe("notifs");
|
streams.subscribe("notifs");
|
||||||
streams.event("notifs", "message", (data) => {
|
streams.event("notifs", "message", (data) => {
|
||||||
if (data === "Ping") {
|
if (!data.method.Packet) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const json = JSON.parse(data);
|
|
||||||
if (!json.method.Packet) {
|
|
||||||
console.warn("notifications stream cannot read this message");
|
console.warn("notifications stream cannot read this message");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const inner_data = JSON.parse(json.data);
|
const inner_data = JSON.parse(data.data);
|
||||||
if (json.method.Packet.Crud === "Create") {
|
if (data.method.Packet.Crud === "Create") {
|
||||||
const current = Number.parseInt(element.innerText || "0");
|
const current = Number.parseInt(element.innerText || "0");
|
||||||
|
|
||||||
if (current <= 0) {
|
if (current <= 0) {
|
||||||
|
@ -293,7 +288,7 @@
|
||||||
|
|
||||||
console.info("notification created");
|
console.info("notification created");
|
||||||
}
|
}
|
||||||
} else if (json.method.Packet.Crud === "Delete") {
|
} else if (data.method.Packet.Crud === "Delete") {
|
||||||
const current = Number.parseInt(element.innerText || "0");
|
const current = Number.parseInt(element.innerText || "0");
|
||||||
|
|
||||||
if (current - 1 <= 0) {
|
if (current - 1 <= 0) {
|
||||||
|
|
|
@ -12,6 +12,14 @@
|
||||||
return $.STREAMS[stream];
|
return $.STREAMS[stream];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
self.define("get", ({ $ }, id) => {
|
||||||
|
for (const stream of Object.values($.STREAMS)) {
|
||||||
|
if (stream.id === id) {
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
self.define("subscribe", ({ $ }, stream) => {
|
self.define("subscribe", ({ $ }, stream) => {
|
||||||
if (!$.USER) {
|
if (!$.USER) {
|
||||||
console.warn("cannot subscribe without user id");
|
console.warn("cannot subscribe without user id");
|
||||||
|
@ -27,6 +35,7 @@
|
||||||
const socket = new WebSocket(endpoint);
|
const socket = new WebSocket(endpoint);
|
||||||
|
|
||||||
$.STREAMS[stream] = {
|
$.STREAMS[stream] = {
|
||||||
|
id: null,
|
||||||
socket,
|
socket,
|
||||||
events: {
|
events: {
|
||||||
message: () => {},
|
message: () => {},
|
||||||
|
@ -38,10 +47,17 @@
|
||||||
return socket.send("Pong");
|
return socket.send("Pong");
|
||||||
}
|
}
|
||||||
|
|
||||||
return $.sock(stream).events.message(event.data);
|
const data = JSON.parse(event.data);
|
||||||
|
|
||||||
|
if (data.method.Forward === "Key") {
|
||||||
|
$.STREAMS[stream].id = data.data;
|
||||||
|
return console.info(`${stream} ${data.data}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $.sock(stream).events.message(data);
|
||||||
});
|
});
|
||||||
|
|
||||||
return socket;
|
return $.STREAMS[stream];
|
||||||
});
|
});
|
||||||
|
|
||||||
self.define("close", ({ $ }, stream) => {
|
self.define("close", ({ $ }, stream) => {
|
||||||
|
|
|
@ -456,6 +456,18 @@ pub async fn handle_socket(socket: WebSocket, db: DataManager, user_id: String,
|
||||||
// get channel permissions
|
// get channel permissions
|
||||||
let channel = format!("{user_id}/{stream_id}");
|
let channel = format!("{user_id}/{stream_id}");
|
||||||
|
|
||||||
|
// identify socket
|
||||||
|
sink.send(WsMessage::Text(
|
||||||
|
serde_json::to_string(&SocketMessage {
|
||||||
|
method: SocketMethod::Forward(PacketType::Key),
|
||||||
|
data: socket_id.clone(),
|
||||||
|
})
|
||||||
|
.unwrap()
|
||||||
|
.into(),
|
||||||
|
))
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
let mut recv_task = tokio::spawn(async move {
|
let mut recv_task = tokio::spawn(async move {
|
||||||
while let Some(Ok(WsMessage::Text(text))) = stream.next().await {
|
while let Some(Ok(WsMessage::Text(text))) = stream.next().await {
|
||||||
|
|
|
@ -14,6 +14,8 @@ pub enum PacketType {
|
||||||
Text,
|
Text,
|
||||||
/// A CRUD operation.
|
/// A CRUD operation.
|
||||||
Crud(CrudMessageType),
|
Crud(CrudMessageType),
|
||||||
|
/// A text key which identifies the socket.
|
||||||
|
Key,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, PartialEq, Eq)]
|
#[derive(Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue