pbm socket connection resolved

This commit is contained in:
simplonco
2023-01-08 14:53:09 +01:00
parent c5ed704a62
commit 4c0256e7b3
7 changed files with 993 additions and 961 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@@ -6,15 +6,15 @@
/* web sockets with socket.io
*/
import { socket, user } from './Chat_socket.svelte';
// import { init_socket, socket, user } from './Socket';
//import { socket, user } from './Chat_socket.svelte';
import { init_socket, socket, user } from './Socket';
import { msgs } from './Store_msg.js';
// async function socket_actions()
// {
//
// await init_socket();
// init_socket();
init_socket();
/*
async function socket_actions()
{
await init_socket();
// pbm: sometimes socket is still undefined here
socket.on('connect', function(){
@@ -61,9 +61,10 @@
from = "me";
msgs.update(msgs => [...msgs, { content: message, name: from }]);
});
// };
};
// socket_actions();
socket_actions();
*/
</script>

View File

@@ -4,6 +4,7 @@
import { onMount } from 'svelte';
import Button from './Chat_button.svelte';
import { msgs } from './Store_msg.js';
import { user } from './Chat_socket.svelte';
export let layout;
let rooms = [
@@ -32,15 +33,15 @@
.then(data =>
{
console.log(data.messages);
data.messages.forEach(function(item) {
if (item.name === user.username) {
item.name = "me";
}
});
msgs.update(msgs => msgs.concat(data.messages));
});
layout = "room";
}
function test() {
console.log("test");
}
/*
*/
</script>

View File

@@ -12,9 +12,9 @@
let msg = "";
let text_area;
function add_local_msg(name, message)
function add_local_msg(message)
{
msgs.update(msgs => [...msgs, { name: name, message: message }]);
msgs.update(msgs => [...msgs, { name: "me", message: message }]);
}
function send_msg()
@@ -22,7 +22,7 @@
msg = msg.trim();
if (msg.length > 0) {
socket.emit('message', msg);
add_local_msg("me", msg);
add_local_msg(msg);
}
msg = "";

View File

@@ -3,26 +3,55 @@ import io from 'socket.io-client';
export let user;
export let socket;
export let temp = "foo";
export function change_temp(){temp = "bar";};
const address = `http://${process.env.WEBSITE_HOST}:${process.env.WEBSITE_PORT}`;
export function init_socket()
export async function init_socket()
{
fetch(`${address}/api/v2/user`)
.then((resp) => resp.json())
.then((data) =>
console.log("inside init_socket");
const response = await fetch(`${address}/api/v2/user`);
const response_data = await response.json();
user = response_data;
console.log("user:", user);
socket = await io(address,
{
user = data;
socket = io(address,
path: '/chat',
query:
{
path: '/chat',
query:
{
username: user.username,
},
})
username: user.username,
},
})
console.log("socket:", socket);
connection_states();
socket_events();
}
function socket_events()
{
socket.on('message', function(from, message)
{
console.log("received msg:", message, from);
if (from === user.username)
from = "me";
msgs.update(msgs => [...msgs, { content: message, name: from }]);
});
}
function connection_states()
{
socket.on('connect', function(){ console.log("socket.io connected"); });
socket.on('disconnect', function(){ console.log("socket.io disconnected"); });
socket.on('connect_error', function(){ console.log("socket.io connect_error"); });
socket.on('connect_timeout', function(){ console.log("socket.io connect_timeout"); });
socket.on('error', function(){ console.log("socket.io error"); });
socket.on('reconnect', function(){ console.log("socket.io reconnect"); });
socket.on('reconnect_attempt', function(){ console.log("socket.io reconnect_attempt"); });
socket.on('reconnecting', function(){ console.log("socket.io reconnecting"); });
socket.on('reconnect_error', function(){ console.log("socket.io reconnect_error"); });
socket.on('reconnect_failed', function(){ console.log("socket.io reconnect_failed"); });
socket.on('ping', function(){ console.log("socket.io ping"); });
socket.on('pong', function(){ console.log("socket.io pong"); });
}