G
G
gleendo2017-07-31 16:05:47
Node.js
gleendo, 2017-07-31 16:05:47

Why can't it find the path... 404 not found (socket.io)?

I continue to read the book on Node.js. Came to web sockets. Something is not very clear. And besides, the examples do not work.
What could be the problem? On startup, the following appears in the console:

GET http://localhost:8080/index.html 404 (Not Found)

// server

let io = require("socket.io").listen(8080);

io.sockets.on("connection", socket => {
  socket.on("message", msg => {
    let time = (new Date).toLocaleTimeString();

    socket.send("Hello " + msg + "!");

    socket.broadcast.send(time + " К нам присоединился " + msg);

    console.log(msg + " connect! " + time);
  });
});

<!-- client -->

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="http://localhost:8080/socket.io/socket.io.js"></script>
</head>
<body>
  Введите ник: <input type="text" id="nickname">
  <input type="button" value="Start chat" id="startbutton">

  <script>
    window.onload = () => {
      document.querySelector("#startButton").addEventListener("click", event => {
        iochar(document.querySelector("#nickname").value);
      }, false);
    }


    const iochat = nick => {
      socket = io.connect("http://localhost:8080");

      socket.on("connect", () => {
        socket.send(nick);

        socket.on("message", msg => {
          alert(msg);
        });
      });
    };

  </script>
</body>
</html>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Wolf, 2017-07-31
@evgeniy8705

You started the socket server, but who will run the HTTP server?
Usually they start an HTTP server, and socket.io sits at some address, which, with the help of Connection-Upgrade, is already moving to WS.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question