Html Socket.IO - 未定义要求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8202053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:35:56  来源:igfitidea点击:

Socket.IO - require is not defined

htmlnode.jssocket.io

提问by Johan Svensson

I'm trying to get socket.io working but now in Chrome I get the error:

我正在尝试使 socket.io 正常工作,但现在在 Chrome 中出现错误:

Uncaught ReferenceError: require is not defined

未捕获的 ReferenceError:需要未定义

client.php:9Uncaught ReferenceError: io is not defined

client.php:9Uncaught ReferenceError: io 未定义

I changed the way to include the socket.io.js file because it dosnt exists somewher else:

我改变了包含 socket.io.js 文件的方式,因为它不存在于其他地方:

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

If I try

如果我尝试

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

I get: Failed to load resource: the server responded with a status of 404 (Not Found)

我得到:无法加载资源:服务器响应状态为 404(未找到)

This is on Ubuntu with the latest of everything

这是在 Ubuntu 上,拥有最新的一切

I'm using the server code from http://socket.io/to work with in the same folder like client.php and that works, named server.js, only modified port.

我正在使用来自http://socket.io/的服务器代码在与 client.php 相同的文件夹中使用,并且该文件夹有效,名为 server.js,仅修改端口。

回答by thejh

If your script isn't coming from your webserver, this will not work:

如果您的脚本不是来自您的网络服务器,这将不起作用:

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

You have to explicitely state host and port:

您必须明确说明主机和端口:

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

回答by Ricardo Tomasi

Are you running node.jsalong PHP on your server?

您是否在服务器上沿 PHP运行node.js

There are two "socket.io" packages, a serverand a client. You're trying to load the serverone (/node_modules/socket.io/lib/socket.io.js) in the browser. The script you want is called socket.io-clientand you can find it at https://raw.github.com/LearnBoost/socket.io-client/master/socket.io-client.js

有两个“socket.io”包,一个server和一个client。您正在尝试在浏览器中加载服务器一 ( /node_modules/socket.io/lib/socket.io.js)。你想要的脚本叫做socket.io-client,你可以在https://raw.github.com/LearnBoost/socket.io-client/master/socket.io-client.js找到它

What happens is that socket.io automatically serves the /socket.io/socket.io.js(the client one) file from port 80 when you're running node on port 80. In your case Apache is already at port 80, so you need to serve the file from it manually.

当您在端口 80 上运行节点时,socket.io 会自动/socket.io/socket.io.js从端口 80提供(客户端)文件。在您的情况下,Apache 已经在端口 80,因此您需要手动提供文件。

回答by Bob

Your library include (/socket.io/socket.io.js) in your client browser coding was probably OK. But you may be pointing to the wrong copy of socket.io.jsIf you installed socket.iousing (npm install socket.io) then you might want to look in the following directory for the client version of socket.io.js:

您的库/socket.io/socket.io.js在您的客户端浏览器编码中包含 ( ) 可能没问题。但是您可能指向了错误的副本socket.io.js如果您socket.io使用 ( npm install socket.io)安装,那么您可能需要在以下目录中查找 的客户端版本socket.io.js

C:\Program Files (x86)\nodejs\node_modules\socket.io\node_modules\socket.io-client\dist\socket.io.js

C:\Program Files (x86)\nodejs\node_modules\socket.io\node_modules\socket.io-client\dist\socket.io.js

If its there, then you may want to copy that module to your web publishing directory, OR as an alternative, you could change the physical path that your virtual directory points to.

如果在那里,那么您可能希望将该模块复制到您的 Web 发布目录,或者作为替代方案,您可以更改虚拟目录指向的物理路径。

回答by JosephK

As others have noted, when io is run on the server side, an "automagical" service of "socket.io.js" is served - and it branches out to the other js files in its lib directory.

正如其他人所指出的,当 io 在服务器端运行时,会提供“socket.io.js”的“自动”服务 - 它会分支到其 lib 目录中的其他 js 文件。

What I wish to add, is that This is Not Intutive and, no, you cannot have your regular webserver serve this file - the errors just get more strange and seemingly contradictory (require not defined, etc). This is not a normal js file, and I wish all the tutorials out there would emphasize that.

我想补充的是,这不是直观的,不,您不能让您的常规网络服务器提供此文件 - 错误只会变得更加奇怪且看似矛盾(需要未定义等)。这不是一个普通的 js 文件,我希望那里的所有教程都会强调这一点。

Anyhow, if you are running behind nginx, for example, set the port-settings to the same port assigned for node - port 3000 in my case. I used that port in the client like this:

无论如何,例如,如果您在 nginx 后面运行,请将端口设置设置为为节点分配的相同端口 - 在我的情况下为端口 3000。我在客户端中使用了该端口,如下所示:

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

... and then also used port 3000 in the server code - there are too many ways / versions you can code the server-js-code for an example to do anything but confuse most who are using 'a different way'.

...然后还在服务器代码中使用了端口 3000 - 有太多的方法/版本你可以编写 server-js-code 作为示例来做任何事情,但会混淆大多数使用“不同方式”的人。

回答by Scott Grodberg

I tried what thejh says and it works. I needed to specify the host and port. For a node app listening on port 81, I needed to specify port 81 in the client javascript. Nevermind /socket.io/socket.io.js doesn't exist in the vhost, node makes up for it at runtime.

我尝试了thejh所说的并且有效。我需要指定主机和端口。对于侦听端口 81 的节点应用程序,我需要在客户端 javascript 中指定端口 81。没关系 /socket.io/socket.io.js 在 vhost 中不存在,节点在运行时弥补它。

回答by James Powell

Actually I was successful in getting Apache to serve up these files, today in fact.

实际上,今天我成功地让 Apache 提供了这些文件。

Simply create a copy (I actually copied the files, soft links might be ok I did not try) of the contents of node_modules/socket.io/node_modules/socket.io-client/dist to your web root. For my install of socket.io (0.9.16) where I installed socket.io globally these two commands did the trick:

只需创建一个 node_modules/socket.io/node_modules/socket.io-client/dist 内容的副本(我实际上复制了文件,软链接可能没问题,我没有尝试)到您的 Web 根目录。对于我在全局安装 socket.io 的 socket.io (0.9.16) 的安装,这两个命令可以解决问题:

sudo cp -av /usr/lib/node_modules/socket.io/node_modules/socket.io-client/dist /var/www/socket.io 
chown -R www-data:www-data /var/www/socket.io

This was on a Ubuntu 13.10 box the usual LAMP stack installed before I then added node.js.

这是在 Ubuntu 13.10 机器上安装的常用 LAMP 堆栈,然后我添加了 node.js。

Now keeping this copy in sync as new versions of socket.io are released is a problem I have not yet tried to address yet.

现在,在新版本的 socket.io 发布时保持此副本同步是我尚未尝试解决的问题。

Cheers

干杯

回答by a paid nerd

If you see something like require() is not defined, it may be because the JavaScript library you're using is in the format of an AMD (Asynchronous Module Definition). Try using RequireJS(full-featured) or the curl JS library(smaller, simpler) to load SocketIO instead.

如果您看到类似 的内容require() is not defined,则可能是因为您使用的 JavaScript 库采用AMD(异步模块定义)格式。尝试使用RequireJS(全功能)或curl JS 库(更小、更简单)来加载 SocketIO。

回答by amateur barista

I had the same problem, and I confirm that @thejh's solution worked. I however was unsure as to "what" was the server when I read his recommendation. I do have MAMP running on port 80. And the code below would be the "node.js server", which runs on port 8001.

我遇到了同样的问题,我确认@thejh 的解决方案有效。然而,当我阅读他的建议时,我不确定服务器“是什么”。我确实在端口 80 上运行了 MAMP。下面的代码将是“node.js 服务器”,它在端口 8001 上运行。

Once I started the node.js server (ran the code below), a visit to http://localhost:4001/socket.io/socket.io.jsreturns the javascript file.

一旦我启动了 node.js 服务器(运行下面的代码),访问http://localhost:4001/socket.io/socket.io.js将返回 javascript 文件。

/* Node.js server */

var sys  = require('sys');
var io   = require('socket.io');
var http = require('http')
server = http.createServer(function(req, res) { 
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write('http server started');
  res.end(); 
});
server.listen(4001);


// socket.io 
var socket = io.listen(server); 
socket.on('connection', function(client) {

  client.on('message', function(message) {
    // We're in!
    console.log('received client message '+ message);
  }); 
  client.on('disconnect', function() {});
  socket.send('welcome to the local node.js server!');
});

For those within Drupal, a drupal_add_js('http://localhost:4001/socket.io/socket.io.js', array('type' => 'external', 'group' => JS_LIBRARY));within a hook_initdoes the trick.

对于那些在 Drupal 中的人来说,adrupal_add_js('http://localhost:4001/socket.io/socket.io.js', array('type' => 'external', 'group' => JS_LIBRARY));中的一个hook_init就行了。