wip chat in room, now broadcast with name

This commit is contained in:
simplonco
2023-01-04 21:57:01 +01:00
parent 1c2d202ec1
commit a4a2b4bf76
9 changed files with 96 additions and 64 deletions

View File

@@ -9,7 +9,8 @@
*/
import { socket } from './Chat_socket.svelte';
onMount(async => {
onMount(async() => {
await socket;
socket.on('connect', function(){
console.log("socket.io connected");
});

View File

@@ -1,17 +1,25 @@
<script context="module">
import io from 'socket.io-client';
export let user;
export let socket;
const address = `http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}`;
export const socket = io(address, {
path: '/chat'
});
const userPomise = fetch(`${address}/api/v2/user`).then((x) => x.json());
export let user
userPomise.then((data) => {
fetch(`${address}/api/v2/user`)
.then((resp) => resp.json())
.then((data) =>
{
user = data;
socket = io(address,
{
path: '/chat',
query:
{
username: user.username,
},
});
});
</script>

View File

@@ -5,10 +5,10 @@
import Msg from './Chat_msg.svelte';
import { socket, user } from './Chat_socket.svelte';
socket.on('message', (data) => {
console.log("received msg :");
console.log(data);
add_msg("other", data);
socket.on('message', (name, message) => {
console.log(name)
console.log(message)
add_msg(name, message);
});
export let layout = "";
@@ -18,17 +18,15 @@
let text_area;
let msgs = [];
function add_msg(from, the_msg)
function add_msg(from, message)
{
msgs = [...msgs, { content: the_msg, name: from }];
if (from === user.username)
from = "me";
msgs = [...msgs, { content: message, name: from }];
}
function send_msg()
{
console.log("user:");
console.log(user);
console.log(user.username);
msg = msg.trim();
if (msg.length > 0) {
socket.emit('message', msg);