节点服务器,Socket.io 'io 未定义'? [英] Node server, Socket.io 'io is not defined'?

查看:50
本文介绍了节点服务器,Socket.io 'io 未定义'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了之前一直对我有用的完全相同的步骤,通过 express 创建应用程序,将模块依赖项放在 node_modules 文件夹中.似乎没有找到 socket.io 客户端 javascript 文件.

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. It appears that the socket.io client-side javascript file isn't being found.

(我查看了其他人的修复程序,即将 JavaScript 文件包含在脚本选项卡中.对于我之前的 node + socket.io 项目,我不必这样做).

(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:

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

服务器上的 JavaScript:

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

node_modules 文件夹:

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

错误信息:

Uncaught ReferenceError: io is not defined
(anonymous function)

当我手动包含 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>

在设置 socket 变量之前?

这篇关于节点服务器,Socket.io 'io 未定义'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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