S
S
scar172021-11-06 00:53:50
Socket.io
scar17, 2021-11-06 00:53:50

Socket IO data transfer?

How to make the console send data to the server? I tried:
Client:

var script = document.createElement("script");
script.src = "https://cdn.socket.io/socket.io-1.2.0.js"
document.body.appendChild(script);

script.onload = function() {
  socket = io("wss://server-bots-owop.glitch.me");
  socket.on("botsJoined", function(x, y){
    return "hi";
  });
};

but it throws an error: Uncaught ReferenceError: io is not defined
Server:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = 3000;

io.on('connection', function(socket){ // Когда на сервер выполняется подключение идет функция:
  socket.on('123', function(){ // Когда с клиента на сервер отправляется информация 123, он делает:
    io.emit('botsJoined'); // Имитирует ивент на стороне клиента, что боты зашли.
  });
});

http.listen(port, function(){
  console.log('listening on *:' + port);
});

If something is wrong, or I made a mistake or messed up something, correct it and write an answer.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question