【发布时间】:2014-11-27 18:42:18
【问题描述】:
我一直在努力设置 socket.io npm 模块,并且我的浏览器能够与后端 NodeJS 脚本交互。但我不确定我哪里出错了。我记得以前在控制台中看到过这样的消息:
“Socket.io .. 已启动 ..”
但现在什么也没有发生。下面是我的 NodeJS 代码:
var express = require('express');
var server = require('http').createServer(express);
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
global.socket = require('socket.io')(server);
//global.socket = require('socket.io').listen(80, {log:true});
//socket.listen(80, {log:true});
在我的 index.js 文件中,我有一个捕获 socket.io 传入连接的函数。当我从浏览器尝试以下 URL 时:
http://localhost:3000/socket.io/socket.io.js
我收到一条错误消息:
Error: Failed to lookup view "error" in views directory "C:\amr\public" at Function.app.render
但是当我请求以下网址时:
http://localhost/socket.io/socket.io.js
浏览器上好像渲染了socket.io.js文件:
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.io=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){
我不确定如何从浏览器或后端查看我的网络套接字是否正常工作,我必须在我的 HTML 页面中包含什么脚本 URL 才能使连接正常工作?
<script src="http://localhost:80/socket.io/socket.io.js"></script>
【问题讨论】: