简体   繁体   English

节点服务器,Socket.io 'io 未定义'?

[英]Node server, Socket.io 'io is not defined'?

I've followed the exact same steps which have always previously worked for me, create application through express, place the module dependencies in the node_modules folder.我遵循了以前一直对我有用的完全相同的步骤,通过 express 创建应用程序,将模块依赖项放在 node_modules 文件夹中。 It appears that the socket.io client-side javascript file isn't being found.似乎没有找到 socket.io 客户端 javascript 文件。

(I've looked at other peoples fixes, which is to include the JavaScript file in a script tab. I have not had to do this for my previous node + socket.io projects). (我查看了其他人的修复程序,即在脚本选项卡中包含 JavaScript 文件。我以前的 node + socket.io 项目不必这样做)。

JavaScript on client:客户端上的 JavaScript:

var socket = io.connect('http://localhost');

JavaScript on server:服务器上的 JavaScript:

var io = require('socket.io').listen(app);

node_modules folder: node_modules 文件夹:

socket.io, which has an internal node_modules folder containing socket.io-client

Error Message:错误信息:

Uncaught ReferenceError: io is not defined
(anonymous function)

When I include the socket.io client manually: http://cdn.socket.io/stable/socket.io.js当我手动包含 socket.io 客户端时: http : //cdn.socket.io/stable/socket.io.js

I get a different error which is:我得到一个不同的错误是:

Uncaught TypeError: Object #<Object> has no method 'connect'
(anonymous function)

On the client, did you do:在客户端,您是否执行了以下操作:

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

before you set the socket variable?在设置socket变量之前?

Node.js newbie here! Node.js 新手来了! I am pretty sure this has been answered.我很确定这已经得到了回答。 Yet I kept finding problems with the src for the socket.但是我一直在寻找套接字 src 的问题。 Apparently this : <script src="/socket.io/socket.io.js"> was not working for me on the client side.显然这个: <script src="/socket.io/socket.io.js">在客户端对我不起作用。

I've replaced the above line with this and it seems to work fine.我已经用这个替换了上面的行,它似乎工作正常。

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

(Edit: although this is obvious, this link may not work depending on when you are reading this answer. Please pick the latest link from: https://cdnjs.com/libraries/socket.io ) (编辑:虽然这很明显,但根据您阅读此答案的时间,此链接可能不起作用。请从以下位置选择最新链接: https : //cdnjs.com/libraries/socket.io

Here is a working client side code:这是一个有效的客户端代码:

<body>

<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>

<script>
    $(function(){
        var socket = io('http://localhost:8080');
        console.log("Socket connected"+socket.connected);

        socket.on('notification', function(value){
            //insert your code here
        });
    });

</script>

On the server side (handles only 1 socket)在服务器端(仅处理 1 个套接字)

var app  = require('express')();
var http = require('http').Server(app);
var io   = require('socket.io')(http);
var port = process.env.PORT || 8080;


app.get('/', function(req, res){
    console.log("app works");
});

io.on('connection', function(socket){
    socket.emit('notification', {message:"hi"});
});

http.listen(port, function(){
    console.log('listening on :' + port);
});

I managed to blunder through this, and squandered about an hour, on something that turned out to be a very basic error.我设法犯了这个错误,浪费了大约一个小时,结果证明这是一个非常基本的错误。

When an function is not defined?什么时候没有定义函数? Such as " Uncaught ReferenceError: io is not defined ".比如“Uncaught ReferenceError: io is not defined”。 Does that not mean that the function is getting "used" before it is "created"?这不意味着函数在“创建”之前就被“使用”了吗?

In the part of my HTML file, that "calls" the javaScript files, it look like this :在我的 HTML 文件的一部分中,它“调用”了 javaScript 文件,它看起来像这样:

<script src='./js/playerChatter.js'></script> <!-- this one calls io -->
<script src="http://localhost:2019/socket.io/socket.io.js"></script><!--ThisCreatesio-->

and i changed it to this我把它改成了这个

<script src="http://localhost:2019/socket.io/socket.io.js"></script> <!--ThisCreates io-->
<script src='./js/playerChatter.js'></script> <!-- this on calls io -->

So now the item "io", whether it is an object or function... Is actually getting created before it is getting used :D所以现在项目“io”,无论是对象还是函数......实际上是在它被使用之前被创建:D

Have FUN!玩得开心!

On the client:在客户端:

<head>
<script src="http://localhost/socket.io/socket.io.js"></script>
</head>

I solved this by running the index page by the Node.js server.我通过运行 Node.js 服务器的索引页解决了这个问题。

Most likely "index.html" and "/socket.io/socket.io.js" files should be served by your Node.js server.最有可能的“index.html”和“/socket.io/socket.io.js”文件应该由你的 Node.js 服务器提供。

as mentioned in the url Uncaught ReferenceError: io is not defined如 url 中提到的Uncaught ReferenceError: io is not defined

So when I executed https://localhost:3000 (3000 the port where the nodejs has been started) the error got resolved.因此,当我执行https://localhost:3000 (nodejs 启动的端口为 3000)时,错误得到了解决。

在此处输入图片说明

在此处输入图片说明

  1. Use server.listen() can fixed it;使用 server.listen() 可以修复它;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM