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

2019-02-11 12:31发布

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

io is not defined

How to fix this ?

7条回答
何必那么认真
2楼-- · 2019-02-11 13:01
<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.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-11 13:01

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

查看更多
欢心
4楼-- · 2019-02-11 13:03

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.

查看更多
5楼-- · 2019-02-11 13:11

Alternatively you can use the Socket.io CDN:

<script src="https://cdn.socket.io/socket.io-1.0.0.js"></script>
查看更多
姐就是有狂的资本
6楼-- · 2019-02-11 13:21

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>
查看更多
劳资没心,怎么记你
7楼-- · 2019-02-11 13:26

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

查看更多
登录 后发表回答