W
W
Wasya UK2019-01-23 01:35:08
Node.js
Wasya UK, 2019-01-23 01:35:08

How to connect to the server in websockets (socket.io)?

I have a Heroku site and I know my IP. The server is running on my PC. How to connect socket.io on a website to it?

<script src="/socket.io/socket.io.js"></script>
  <script>
    let socket = io(); // может тут надо написать? если да, то как примерно должно выглядеть?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wasya UK, 2019-01-23
@dmc1989

const socket = io('https://' + '255.255.1.1' + ':' + 443);

S
sorokinfil, 2019-01-23
@sorokinfil

On the client. the url at which the socket is available, usually in the form ip:port or domain:port; port is usually 3000, but can be anything. It is not necessary to send an object, it is enough to track the events transmitted to emit as the first parameter. The syntax for emit and on is the same on the server and client.

var socket_url = "yoursite.ru:3000";
var socket = io.connect (socket_url);

// Отправка чего-то на сервер
socket.emit ('Privet',
{
  param1: 'kaknikruti',
  param2: 12345
  // и так далее
});

// Приём с сервера
socket.on ('Otvet', function (data)
{
  alert (data.otvet);
});

Judging by the description, the client in this case is your cloud server. But the procedure should not be different.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question