`require'socket.io-client.js'`不起作用 [英] `require 'socket.io-client.js'` not working

查看:347
本文介绍了`require'socket.io-client.js'`不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在自己的服务器上运行基本的socket.io服务器应用程序,并直接通过任何Web浏览器(我尝试使用FF,chrome和IE7都可以运行)直接请求它.

I was able to get the basic socket.io server application running on my own server, and request it directly through any web browser (I tried FF, chrome, and IE7 which all worked).

现在,问题出在客户端示例代码对我不起作用,并且在chrome的javascript控制台中出现以下错误:

Now, the issue comes in that the client sample code doesn't work for me, and I get the following error in the javascript console in chrome:

参考socket.io.js中的这一行代码,出现未捕获的ReferenceError:require未定义": var client = require('socket.io-client');

"Uncaught ReferenceError: require is not defined" in reference to this line of code in socket.io.js: var client = require('socket.io-client');

这使我相信它无法识别require命令周期,这似乎很奇怪.其他几件事-我正在运行apache,因此将我的所有socket.io文件都移到了我的apache目录htdocs中,可以通过使用cygwin和以下指南安装的http端口80进行访问:

This leads me to believe that it doesn't recognize the require command period, which seems odd. A couple of other things - I have apache running, and so moved all of my socket.io files into my apache directory htdocs to be accessed through http port 80 which were installed using cygwin and the guide at: https://github.com/joyent/node/wiki/Building-node.js-on-Cygwin-(Windows)

socket.io文件也安装在Windows的c:驱动器上的cygwin目录下,如果通过apache访问,则在这里没有用.另一个花絮-我确实有一个socket.io-client.js文件,但是当我使用写字板打开该文件进行编辑时,它看起来已损坏,里面只有一行文本:<symlink>ÿþi

The socket.io files were also installed under the cygwin directory on my c: drive in windows, where they are not useful if accessed by apache. One other tidbit - I do have a socket.io-client.js file, but when I opened it to edit using wordpad, it looks corrupted, having only one line of text inside: <symlink>ÿþi

推荐答案

require()函数是Node.js的功能,仅在服务器端运行的Javascript上有效.要在浏览器中包含文件,您必须使用常规方法:

The require() function is a feature of Node.js and only works on the Javascript that is run on the server side. To include files in the browser, you would have to use the regular method:

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

Node.js通常以将socket.io服务器连接到Web服务器实例的方式进行设置,该实例也是Node.js服务器的一部分.直接从 socket.io如何使用" 页面获取的代码示例,在服务器端:

Node.js is usually set up in a way that the socket.io server is attached to an instance of web server, which is also part of the Node.js server. Code examples taken directly from the socket.io "how to use" page, this would be on the server side:

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')
app.listen(80);

如果按上述方式使用,则Node.js是服务器,还提供网页的静态部分,而Node.js服务器的地址是包含客户端脚本的参考.

If used as above, Node.js is the server that also serves the static part of the web page, and address of Node.js server is the reference for including client side scripts.

另一个用例是当静态html由您的主Web服务器提供服务并且您试图连接到可能位于另一个地址或另一个端口或两者中的Node.js实例时.主Web服务器不提供Socket.io.js.它由Node.js服务器上运行的socket.io直接提供.您必须提供客户端浏览器的Node.js服务器地址,以获取socket.io客户端Javascript文件,如下所示:

Another use case is when the static html is served by your main web server and you are trying to connect to a Node.js instance that might be in another address or another port or both. Socket.io.js is not served by your main web server. It is served directly by the socket.io running on the Node.js server. You have to provide client browser the Node.js servers address to get the socket.io client side Javascript file, like this:

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

<script>
  var socket = io.connect('http://nodejs.address:port');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
});
</script>

请注意,有些客户端javascript库提供了require()函数,请检查客户端需要Javascript

As a side note, there are client side javascript libraries that provide require() function, check Javascript require on client side

这篇关于`require'socket.io-client.js'`不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆