我想做这样的事情:
var events = require("events");
var emitterA = new events.EventEmitter();
var emitterB = new events.EventEmitter();
emitterA.addListener("testA", function(){
console.log("emitterA detected testA");
});
emitterB.addListener("testA", function(){
console.log("emitterB detected testA");
});
emitterA.emit("testA");
Run Code Online (Sandbox Code Playgroud)
输出是这样的:
emitterA detected testA
emitterB detected testA
Run Code Online (Sandbox Code Playgroud)
但是当我运行这段代码时,我得到的输出是:
emitterA detected testA
Run Code Online (Sandbox Code Playgroud)
我基本上希望一个发射器监听另一个发射器发射的事件。
基本原理:我正在 Node 上编写一个服务器,服务器发送 Server Sent Events。这意味着我需要持久连接。为了确保连接不会因某种原因关闭(我不想将超时设置为无穷大,因为这感觉像是黑客攻击并且看起来不安全),我发送了一个只有空白数据的心跳,只是这样正在传递给浏览器。
我正在运行一个计时器,并且每个时间段(在我的测试用例中为 1 秒),它都会触发,server.emit("hb");之后我会同时向所有请求对象写入和发送数据(在我的笔记本电脑上,这只是多个选项卡和多个浏览器)。所以基本上req.on("hb", callback)
这对我来说似乎更清晰,因为另一种方法是为每个请求对象提供自己的计时器,这意味着将有许多计时器在各处运行,每个计时器都会导致其各自的请求对象发出心跳事件。这似乎是一种非最佳的做事方式。
另外,因为每个请求对象都是自发创建和销毁的,这样做基本上可以确保监听器也被创建和销毁。
所以我想要的是服务器发出的心跳事件,所有活动的请求对象都会听到它并沿着它们自己的连接写入数据。
另一种选择(我现在已经开始工作)是让服务器监听它自己的事件,并让服务器编写心跳。这样做的问题是服务器上的 maxListeners 限制——每个请求对象都会向服务器附加一个新的“hb”侦听器,以便服务器可以侦听该特定请求对象的该事件。不过,最大听众数是 10,虽然我也可以将其设置为无穷大,但我不太热衷于这样做,因为我真的很好奇是否有更好的方法。
对我来说最好和最干净的解决方案似乎是让请求对象“订阅”服务器事件。我这样做是为了学习如何在 node 中编程,所以我想实现尽可能少的外部库并利用 node 的原始功能来实现,但是如果它需要一个外部库,我很乐意阅读它源代码只是为了让我可以学习如何实现一个小的本地实现。
但是当我下载脚本并打开演示时,它在我的浏览器上不起作用.我的控制台上没有错误.
我尝试在浏览器中运行相同的脚本,这次我收到错误."未捕获的TypeError:无法读取null turn.js的属性'style':184"
我不太清楚.这是我正在使用的代码.
<html>
<head>
<script type="text/javascript" src="includes/jquery-1.11.0.js"></script>
<script type="text/javascript" src="includes/turn.js"></script>
<script>
$("#flipbook").turn({
width: 400,
height: 300,
autoCenter: true
});
</script>
<style>
body{
overflow:hidden;
}
#flipbook{
width:400px;
height:300px;
}
#flipbook .page{
width:400px;
height:300px;
background-color:white;
line-height:300px;
font-size:20px;
text-align:center;
}
#flipbook .page-wrapper{
-webkit-perspective:2000px;
-moz-perspective:2000px;
-ms-perspective:2000px;
-o-perspective:2000px;
perspective:2000px;
}
#flipbook .hard{
background:#ccc !important;
color:#333;
-webkit-box-shadow:inset 0 0 5px #666;
-moz-box-shadow:inset 0 0 5px #666;
-o-box-shadow:inset 0 0 5px #666;
-ms-box-shadow:inset 0 0 5px #666;
box-shadow:inset 0 0 …Run Code Online (Sandbox Code Playgroud) 我正在阅读指针是如何工作的,因为我正在尝试理解qsort().但是指南给出了这段代码:
// increaser
#include <iostream>
using namespace std;
void increase (void* data, int psize)
{
if ( psize == sizeof(char) )
{ char* pchar; pchar=(char*)data; ++(*pchar); }
else if (psize == sizeof(int) )
{ int* pint; pint=(int*)data; ++(*pint); }
}
int main ()
{
char a = 'x';
int b = 1602;
increase (&a,sizeof(a));
increase (&b,sizeof(b));
cout << a << ", " << b << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
没有解释什么线
pint=(int*)data;
Run Code Online (Sandbox Code Playgroud)
和
pchar=(char*)data;
Run Code Online (Sandbox Code Playgroud)
手段.我理解其余的意思,但对我来说(char*)数据意味着什么是很有意义的.它是否指向char的值?但是,如果char是变量类型,怎么可能呢?
javascript ×2
c++ ×1
css ×1
eventemitter ×1
events ×1
html ×1
jquery ×1
node.js ×1
pointers ×1
turnjs ×1