最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - POST http:localhost:3000socket.io?EIO=3&transport=polling&t=NQUneY3 400 (Bad Request) in ejs and no

programmeradmin3浏览0评论

I am very new to this socket.io.

I have the code this is a node server code:

var express = require("express");
var app = express();
var http = require("http").createServer(app);
var socketIO = require("socket.io")(http);
var socketID = "";

socketIO.on("connection", function (socket) {
  console.log("User is conneected ", socket.id);
  socketID = socket.id;
});

And here is the code for a ejs file:

......

<script src="/public/js/socket.io.js"></script>

<script>
......
var socketIO = io("http://localhost:3000");
......
</script>
...... 

And the socket.io.js file is here:

I tried but nothing is working. The same error pops whenever I refresh the page. I am very new to this and I really want to get it sorted as soon as possible!!

I already have a listen function just after socket.on:

http.listen(3000, function () {
  console.log("Server has started running!!");
  .........................
.............
})

I am very new to this socket.io.

I have the code this is a node server code:

var express = require("express");
var app = express();
var http = require("http").createServer(app);
var socketIO = require("socket.io")(http);
var socketID = "";

socketIO.on("connection", function (socket) {
  console.log("User is conneected ", socket.id);
  socketID = socket.id;
});

And here is the code for a ejs file:

......

<script src="/public/js/socket.io.js"></script>

<script>
......
var socketIO = io("http://localhost:3000");
......
</script>
...... 

And the socket.io.js file is here:

I tried but nothing is working. The same error pops whenever I refresh the page. I am very new to this and I really want to get it sorted as soon as possible!!

I already have a listen function just after socket.on:

http.listen(3000, function () {
  console.log("Server has started running!!");
  .........................
.............
})
Share Improve this question edited Dec 26, 2020 at 12:54 BhaskerYadav 5895 silver badges24 bronze badges asked Dec 26, 2020 at 11:47 SatyamSatyam 6672 gold badges9 silver badges22 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 4

The below code is working for me.

var express         = require('express');
var app             = express();
var server          = require('http').Server(app);
var io              = require('socket.io')(server);

io.sockets.on('connection', function (socket) {
        console.log(socket);
})

server.listen(3000, function(){
    console.log('listening on *:3000');
});

<script src="/socket.io/socket.io.js" > </script>

<script>
    $(function () {
        var socket = io.connect();
    });
</script>

socket.io.js that you have mentioned is the same as that from the https://cdnjs.cloudflare./ajax/libs/socket.io/2.2.0/socket.io.js... check the installed version of serail io in package.json and place the same version here in the path socket.io/2.2.0 it will work

Please Check versions of socket.io in your Frontend and Backend. It should be patible. I had same issue so I solved it with version change. Like I had 2.2.0 in my frontend so I install 1.7.2 in backend or same as frontend.

Here is my up to date (Jan 2024) solution with Typescript:

server.ts

import express, { Express, Request, Response } from 'express';
import { Server, Socket } from 'socket.io';
import { createServer } from 'http';
import path from 'path';

const app: Express = express();
app.set("port", process.env.PORT || 3000);

const httpServer = createServer(app);
const io = new Server(httpServer, {/* options */});

app.get("/", (req: any, res: any) => {
  res.sendFile(path.resolve("./client/index.html"));
});

io.on("connection", function(socket: Socket) {
  console.log("a user connected");
  socket.on("message", function(message: any) {
  console.log(message);
  });
});

const server = httpServer.listen(3000, function() {
  console.log("listening on *:3000");
});

./client/index.html

<!-- ./client/index.html -->
<!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>
  </head>
  <body>
    <button onClick="sendMsg()">Emit Msg</button>
    <script src="https://cdnjs.cloudflare./ajax/libs/socket.io/4.7.3/socket.io.js"></script>
    <script>
      const socket = io("http://localhost:3000");
      function sendMsg() {
        socket.emit("message", "HELLO WORLD");
      }
    </script>
  </body>
</html>

as was mentioned before it is important that the socket.io version on the server side matches the script src on the served page 4.7.3:

<script src="https://cdnjs.cloudflare./ajax/libs/socket.io/4.7.3/socket.io.js">

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论