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
4 Answers
Reset to default 4The 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">
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744907315a4600334.html
评论列表(0条)