Socket.io JS Client returning connection false - javascript

Im trying to connect the socket in my client js file but the response im getting is always false and no socket.on function is working
<script>
debugger
const socket = io.connect('http://localhost:5000');
console.log('check 1', socket.connected);
socket.on('connect', function () {
console.log('check 2', socket.connected);
});
socket.on("connect_error", (reason) => {
console.log('check 2', reason);
socket.connect();
});
socket.on('disconnect', function () {
console.log('check 2', socket.connected);
});
</script>
No function is running but looking at the network debugger it is returning 200 response . Any help would be apprecaited

Related

How to check consol.log and play sound in JavaScript

I am planning for queue system application using WebSocket ,PHP ,MySQL ,HTML to send console.log from one client to another to play sound for next queue ,
now I want to check in clinet2 if console.log value which I receive it client ='Hello From Client1!' paly sound or if = 'another message' do action.
Client1
<button onclick="sendMessage()">Send Msg</button>
</body>
<script>
// Create WebSocket connection.
const socket = new WebSocket('ws://localhost:3000');
// Connection opened
socket.addEventListener('open', function (event) {
console.log('Connected to WS Server')
});
// Listen for messages
socket.addEventListener('message', function (event) {
console.log('Message from server ', event.data);
});
const sendMessage = () => {
socket.send('Hello From Client1!');
}
</script>
Client2
<button onclick="sendMessage()">Send Msg</button>
</body>
<script>
// Create WebSocket connection.
const socket = new WebSocket('ws://localhost:3000');
// Connection opened
socket.addEventListener('open', function (event) {
console.log('Connected to WS Server')
});
// Listen for messages
socket.addEventListener('message', function (event) {
console.log('Message from server ', event.data);
});
const sendMessage = () => {
socket.send('Hello From Client2!');
}
</script>
Thanks All,
I think I got some code can help me.
I add in Client 1
<script>
var connection = new WebSocket ('ws://localhost:3000');
connection.onmessage = function (event){
document.getElementById("sul").value=event.data;
};
const sendMessage = () => {
socket.send("test");
}
</script>
<output type="text" id="sul" value="" readonly></output>
and client 2
socket.addEventListener('message', function (event) {
console.log('Message from server ', event.data);
if (event.data =='sul 1'){
console.log('Here i can put my play sound');
playsound();
}
});

Why React js receive multiple answer from server with Socket.io?

When emit a event to the websocket server, the server receive the event once and send a response, but the client receive the event multiple time:
result expected
result received
client side:
client.emit("test", {
room: props.room,
});
client.on("testResponse", () => {
console.log("test response app");
});
server side :
socket.on("test", (data: { room: string }) => {
console.log("test");
io.in(data.room).emit("testResponse", {});
});
UPDATE:
on the client side I got to put the socket.on('response,()=>{...}) in the UseEffect of the component and add a socket.off in the return of the useEffect
useEffect(() => {
socket.on("testResponse", () => {
console.log("test response app");
});
return () => {
socket.off("testResponse");
};
});

Websocket is not connecting with javascript

So i'm trying to connect to a websocket server using javascript. I tried doing this with NODEJS with the following code and it worked.
const WebSocket = require('ws');
const ws = new WebSocket('<Websocket Address>', {
origin: '<URL>'
});
ws.on('open', function open() {
ws.send(JSON.stringify({"event":"auth","args":["<My authentication token>"]}))
ws.send(JSON.stringify({"event":"send logs","args":[null]}))
console.log('connected');
});
ws.on('close', function close() {
console.log('disconnected');
});
ws.addEventListener('send logs', function (event) {
console.log('Message from server ', event);
});
ws.on('message', function incoming(data) {
console.log(data);
}, 500);
Is there a way I can write this in plain javascript? Like in script tags? I've read that the following code should work but it's giving me this error Uncaught DOMException: "An invalid or illegal string was specified"
const ws = new WebSocket('<Websocket address>', {
origin: '<URL>'
});
ws.on('open', function open() {
ws.send(JSON.stringify({"event":"auth","args":["<My authentication Token>"]}))
ws.send(JSON.stringify({"event":"send logs","args":[null]}))
console.log('connected');
});
ws.on('close', function close() {
console.log('disconnected');
});
ws.addEventListener('send logs', function (event) {
console.log('Message from server ', event);
});
ws.on('message', function incoming(data) {
console.log(data);
}, 500);
Could anyone tell me what i'm doing wrong?

WebSocket is not defined with NuxtJS

So I want to make a web socket client using a VueJs using NuxtJs framework, this is My component
export default {
data() {
return {
connection: null,
}
},
created() {
console.log("Starting connection to WebSocket Server")
this.connection = new WebSocket("wss://echo.websocket.org")
this.connection.onmessage = function(event) {
console.log(event);
}
this.connection.onopen = function(event) {
console.log(event)
console.log("Successfully connected to the echo websocket server...")
}
},
head() {
return {
title: 'Web Socket',
meta: [
{
hid: 'description',
name: 'description',
content: 'Web socket'
}
]
}
}
}
And I got this message
I'm using Ms Edge for the browser, I tried using a vue-native-socket, and other socket package, but still get the same error 'Websocket not defined'
I'm no Websocket expert, but to my knowledge, this is something available only on client side.
You may try to follow up this answer: https://stackoverflow.com/a/67751550/8816585
created() {
if (process.client) {
// the code in this block will only run on client side
console.log("Starting connection to WebSocket Server")
this.connection = new WebSocket("wss://echo.websocket.org")
this.connection.onmessage = function(event) {
console.log(event);
}
this.connection.onopen = function(event) {
console.log(event)
console.log("Successfully connected to the echo websocket server...")
}
}
}
This will prevent the code written inside of the created() hook to be triggered on both server and client side (hence the error on the server). Because created() is available on both server and client side, as explained here: https://nuxtjs.org/docs/2.x/concepts/nuxt-lifecycle/

NodeJS Cannot receive data from telnet socket after writing to it

when I connect I get the initial data as if I opened the telnet manually from the CMD, but I'm trying to send a command and it doesn't seem to be working as it's not logging the data. I looked everywhere and I'm not sure what to do.
Here is the code I used
const conn = new net.Socket();
conn.connect({port: 587, host: mxOrDomain}, () => {
conn.setEncoding('utf8');
conn.on('data', (data) => {
const message = data.toString();
console.log(message);
});
conn.on('error', (data) => {
console.log('error : ', data);
});
conn.on('drain', () => {
console.log('drained');
});
conn.on('end', () => {
console.log('Ended');
});
setTimeout(() => {
conn.write('HELO blahbalh.com\\r\\n', 'utf8');
}, 2000);
});
Also, is there a reason why telnet {domain} 587 works in CMD and not when using cp.exec() or .spawn() ?
Turns out you have to end your write string with \r\n but my IDE put \\r\\n by default.

Categories

Resources