`需要'socket.io-client.js'` 不工作 [英] `require 'socket.io-client.js'` not working

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

问题描述

我能够在我自己的服务器上运行基本的 socket.io 服务器应用程序,并直接通过任何网络浏览器请求它(我尝试了 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:

"Uncaught ReferenceError: require is not defined" 参考 socket.io.js 中的这行代码: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 以通过 http 端口 80 访问,这些端口是使用 cygwin 和指南安装的:https://github.com/joyent/node/wiki/Building-node.js-on-Cygwin-(Windows)

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 文件也安装在我的 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 实例.Socket.io.js 不是由您的主 Web 服务器提供的.它由运行在 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>

作为旁注,有提供 require() 函数的客户端 javascript 库,请查看 客户端需要Javascript

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

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

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