D
D
Danil Samusev2021-07-18 14:26:55
Node.js
Danil Samusev, 2021-07-18 14:26:55

How to fix Parse Error: Expected HTTP/?

The bottom line is this: I'm trying to make a server on a socket and connect to it from another computer, but something interferes
. Server code:

const WebSocket = require('ws');
var http = require("http");

var port = 80;

const server = http.createServer();

const wss = new WebSocket.Server({ server });

wss.on('connection', function connection(ws) {
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });

  ws.send('something');
});

server.listen(port, function(err) {
    if (err) {
        throw err;
    }
    console.log(`listening on port ${port}!`);
});


Client code:
var WebSocket = require('ws')

var socket = new WebSocket("ws://75.123.130.210:80");

socket.onopen = function() {
  console.log("Соединение установлено.");
};

socket.onclose = function(event) {
  if (event.wasClean) {
    console.log('Соединение закрыто чисто');
  } else {
    console.log('Обрыв соединения'); // например, "убит" процесс сервера
  }
  console.log('Код: ' + event.code + ' причина: ' + event.reason);
};

socket.onmessage = function(event) {
  console.log("Получены данные " + event.data);
};

socket.onerror = function(error) {
  console.log("Ошибка " + error.message);
};

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Danil Samusev, 2021-07-18
@danilka238

I decided. It was about the ports. An error occurred on the VPS hosting and two people were using the same port.

G
Gennady S, 2021-07-18
@gscraft

Why are you passing the http server to the WebSocket server? Just specify the port: - http is superfluous.
const wss = new WebSocket.Server({port: 80});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question