尝试连接到服务器时..失败:WebSocket握手期间出错:意外的响应代码:404

时间:2019-11-13 07:20:04

标签: javascript websocket

  

与'ws:// localhost:8080 / Index.html / ETOM / chat / sara'的WebSocket连接失败:WebSocket握手期间出错:意外的响应代码:404

ws = new WebSocket("ws://" + document.location.host + "/Index.html/ETOM2.0/chat/" + username);

1 个答案:

答案 0 :(得分:0)

更新2020年9月(Express +套接字相同端口)

对于所有新来的东西,请尝试以下方法:

//Back-End Node.js
const app = require("express")(); /*Running it right away*/
const server = require("http").Server(app);/*Use server Function and pass The app To It*/
var io = require("socket.io")(server);
const port = 3000;

app.get("/", (req, res) => {
});


server.listen(port, () => {
 console.log("app listening on port 3000 in");
});


io.on("connection", (socket) => {
  console.log("user connected");
  socket.emit("message", { backend: "hey how are you?" });
  socket.on("another event", (data) => {
   console.log(data);
  });
});

//Front-End ES6
import io from "socket.io-client";
var socket = io("ws://localhost:3000", { transports: ["websocket"] });
socket.on("message", (data) => {
 console.log(data);
 socket.emit("another event", { frontend: "I am great thank you" });
});