开发者

socket.io, io is not defined (JS Error)

开发者 https://www.devze.com 2023-02-25 13:15 出处:网络
I have just started with socket.io, its giving JS Error on client page io is not de开发者_开发百科fined

I have just started with socket.io, its giving JS Error on client page

io is not de开发者_开发百科fined

How to fix this ?


Alternatively you can use the Socket.io CDN:

<script src="https://cdn.socket.io/socket.io-1.0.0.js"></script>


put <script src="http://yournodeserver/socket.io/socket.io.js"></script> into your code


I faced the same problem when using express. Even putting the server:port inside the script would not work.After the server started i would make socket listen to that port, that was mistake i guess.Changing it to below works fine

var app = express();
app.set('port', process.env.PORT || 3000);
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(app.get('port'));

On Client side I just include the script

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


I have a socket app where my server (not a CDN) is serving up the socket.io.js script. So, while Emmerman is right in saying that you need to include the script tag in your client HTML code, the asset won't be loaded if your back-end is down. One option is for you to write a client-side JS script that checks for io before you try and use socket.io. If it's not present (undefined/null) then you can either conditionally show something else like, "server down" or in my case, I'm going to set a timer that keeps checking periodically until the server is restored.

[UPDATE 2] Ended up having to include the script tag, check for existence of io object and doing a window.location.reload() after 10 seconds (using setTimeout) (which eventually will hopefully find that the script loaded and io exists, after which I can connect to the socket server.)

[UPDATE] I'm loading the script with an ajax call rather than using an html script tag. Then with the timer I'm checking periodically if the script will load – eventually it will when the server is restored/rebooted. jQuery ref to load JS scripts dynamically: http://api.jquery.com/jQuery.getScript/


<script src="http://cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></script>

this is the latest version of socket.io to be included.


Your make sure your using "server" instead "app".

const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const {
    Server
} = require('socket.io');
const io = new Server(server);

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html')
});

io.on('connection', (socket) => {
    console.log('a user connected');
});

app.listen(3000, () => {
    console.log('Listining on 3000');
}); // wrong

server.listen(3000, () => {
    console.log('Listining on 3000');
}); // correct


Wrap your client code on a '$(document).ready()' for jQuery or another library similar function. This way you'll be sure your code runs after the library beeing loaded.


I had to do this. (For the client.)

function setup() {
  var socket;
  socket = io.connect('http://localhost:3000');
}
/*
This solved my error.
*/


If your server listening on port 3000 for example and your index.html connect on localhost:3000 then it will fail to include the local libraries on that port. To solve this problem, you should have already installed a normal server for example xampp and include the full url for your library to be served from xampp into your page for example : //localhost/my-project/node_modules/socket.... or just include it from cdn or use socket = io.connect('http://localhost:3000'); and load your page from normal server for example xampp


Change order of scripts files, first library socket.io next yours scripts files

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


No need to edit anything just go to your index.js and make sure that you put :

server.listen(PORT,()=>{
/**Code**/
})


http://socket.io/download/ - the official page for latest cdn.

0

精彩评论

暂无评论...
验证码 换一张
取 消