- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
这就是交易:我正在尝试在一个快速项目中使用 socket.io。在 Express Js 4 发布后,我更新了我的 express-generator,现在应用程序初始函数进入 ./bin/www
文件,包括那些变量(www 文件内容:http://jsfiddle.net/avMa5/)
var server = app.listen(app.get('port'), function() {..}
(通过 npm install -g express-generator
检查它,然后 express myApp
话虽如此,让我们记住 socket.io 文档是如何要求我们触发它的:
var app = require('express').createServer();
var io = require('socket.io')(app);
好的,但我不能在 app.js 中这样做,就像推荐的那样。这应该在 ./bin/www 中完成才能正常工作。在 ./bin/www 中,这是我可以做的让它工作:
var io = require('socket.io')(server)
好的,这可行,但我不能在其他任何地方使用 io var,而且我真的不想将我的 socket.io 函数放在 www
文件中。
我猜这只是基本语法,但我无法让它工作,即使使用 module.exports = server
或 server.exports = server
也不module.exports.io = app(io)
在 www 文件上
所以问题是:如何使用 socket.io 将此/bin/www 文件作为我的应用程序的起点?
最佳答案
以下是将 Socket.io 添加到新生成的 Express-Generator 应用程序的方法:
socketapi.js
:socketapi.js:
const io = require( "socket.io" )();
const socketapi = {
io: io
};
// Add your socket.io logic here!
io.on( "connection", function( socket ) {
console.log( "A user connected" );
});
// end of socket.io logic
module.exports = socketapi;
bin/www
发射器。有两个步骤:需要您的 Socket.io api 并在创建 HTTP 服务器后立即将 HTTP 服务器附加到您的 socket.io 实例:bin/www:
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('socketexpress:server');
var http = require('http');
let socketapi = require("../socketapi"); // <== Add this line
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
socketapi.io.attach(server); // <== Also add this line
(...)
</body>
之前添加以下内容结束标签:index.html
(...)
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
</body>
</html>
DEBUG=myapp:* npm start
set DEBUG=myapp:* & npm start
注 1
如果出于任何原因您需要访问 app.js
中的套接字 API ,那么您可以改为在 app.js
中导入您的套接字 api ,然后重新导出:
app.js
var express = require('express');
var socketapi = require("./socketapi"); // <== Add this line
(...)
// You can now access socket.io through the socketapi.io object
(...)
module.exports = { app, socketapi }; // <== Export your app and re-export your socket API here
然后在你的bin/www
启动器,而不是在自己的行中导入您的套接字 api,只需在您的应用程序中导入它:
bin/www
var {app, socketapi} = require('../app'); // <== Import your app and socket api like this
(...)
var server = http.createServer(app);
socketapi.io.attach(server); // <== You still have to attach your HTTP server to your socket.io instance
注意 2此答案已更新为使用最新的 Express Generator(撰写本文时为 4.16)和最新的 Socket.io(撰写本文时为 3.0.5)。
关于javascript - 在 Express 4 和 express-generator's/bin/www 中使用 socket.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24609991/
基于 socket.io 的官方网站 http://socket.io/#how-to-use , 我找不到任何术语。socket.emit 、 socket.on 和 socket.send 之间有
我正在使用 lua-socket 3.0rc1.3(Ubuntu Trusty 附带)和 lua 5.1。我正在尝试监听 unix 域套接字,我能找到的唯一示例代码是 this -- send std
这两者有什么区别? 我注意到如果我在一个工作程序中从 socket.emit 更改为 socket.send ,服务器无法接收到消息,虽然我不明白为什么。 我还注意到,在我的程序中,如果我从 sock
使用套接字在两台服务器之间发送数据是个好主意,还是应该使用 MQ 之类的东西来移动数据。 我的问题:套接字是否可靠,如果我只需要一次/有保证的数据传输? 还有其他解决方案吗? 谢谢。 最佳答案 套接字
引自 this socket tutorial : Sockets come in two primary flavors. An active socket is connected to a
我已经安装了在端口81上运行的流服务器“Lighttpd”(light-tpd)。 我有一个C程序,它使用套接字api创建的服务器套接字在端口80上监听http请求。 我希望从客户端收到端口80上的请
这是我正在尝试做的事情: 当有新消息可用时,服务器会将消息发送给已连接的客户端。另一方面,客户端在连接时尝试使用send()向服务器发送消息,然后使用recv()接收消息,此后,客户端调用close(
如何将消息发送到动态 session 室,以及当服务器收到该消息时,如何将该消息发送到其他成员所在的同一个 session 室? table_id是房间,它将动态设置。 客户: var table_i
这是我尝试但不起作用的方法。我可以将传入的消息从WebSocket连接转发到NetSocket,但是只有NetSocket收到的第一个消息才到达WebSocket后面的客户端。 const WebSo
我正在实现使用boost将xml发送到客户端的服务器。我面临的问题是缓冲区不会立即发送并累积到一个点,然后发送整个内容。这在我的客户端造成了一个问题,当它解析xml时,它可能具有不完整的xml标记(不
尝试使用Nginx代理Gunicorn套接字。 /etc/systemd/system/gunicorn.service文件 [Unit] Description=gunicorn daemon Af
我正在使用Lua套接字和TCP制作像聊天客户端和服务器这样的IRC。我要弄清楚的主要事情是如何使客户端和服务器监听消息并同时发送它们。由于在服务器上执行socket:accept()时,它将暂停程序,
我看了一下ZMQ PUSH/PULL套接字,尽管我非常喜欢简单性(特别是与我现在正在通过UDP套接字在系统中实现的自定义碎片/ack相比),但我还是希望有自定义负载平衡功能,而不是幼稚的回合-robi
我正在编写一个应用程序,其中有多个 socket.io 自定义事件,并且所有工作正常,除了这个: socket.on("incomingImg", function(data) {
在我的应用程序中,我向服务器发送了两条小消息(类似 memcached 的服务)。在类似 Python 的伪代码中,这看起来像: sock.send("add some-key 0") ignored
很抱歉再次发布此问题,但大多数相关帖子都没有回答我的问题。我在使用 socket.io 的多个连接时遇到问题我没有使用“socket.socket.connect”方法,但我从第一次连接中得到了反馈。
我尝试使用 socket.io 客户端连接到非 socket.io websocket 服务器。但我做不到。我正在尝试像这样连接到套接字服务器: var socket = io.connect('ws
我遇到了一个奇怪的问题。在我非常基本的服务器中,我有: server.listen(8001); io.listen(server); var sockets = io.sockets; 不幸的是,套
我正在使用带套接字 io 的sailsjs。帆的版本是 0.10.5。我有以下套接字客户端进行测试: var socketIOClient = require('socket.io-client');
这个问题在这里已经有了答案: What is the fundamental difference between WebSockets and pure TCP? (4 个答案) 关闭 4 年前。
我是一名优秀的程序员,十分优秀!