我在端口80上运行的node.js中编写了http代理.我只需要将socket.io流量重定向到端口9090,将标准http流量重定向到8080上的Apache.这是我的代理代码:
httpProxy = require('http-proxy');
httpProxy.createServer(function (req, res, proxy) {
if (req.url.match(/socket.io/)) {
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 9090
});
} else {
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 8080
});
}
}).listen(80);
Run Code Online (Sandbox Code Playgroud)
一切正常,但io.socket回归到xhr-polling.
http://localhost/client.htm - falls back to xhr-polling
file:///C:/.../client9090.htm - still uses websocket
Run Code Online (Sandbox Code Playgroud)
socket.io app在端口9090上运行,client.htm连接到80,client9090.htm直接连接到9090.
看起来像node-http-proxy使socket.io应用程序在xhr-polling模式下工作.客户端是Chrome v.25
socket.io应用程序代码
var io = require('socket.io').listen(9090);
io.on('connection', function (socket) {
socket.on('hi!', function (data) {
console.log(data);
socket.emit('news');
});
socket.on('ahoj', function (data) {
console.log(data);
});
});
Run Code Online (Sandbox Code Playgroud)
client.htm代码
<script src="http://localhost/socket.io/socket.io.js"></script>
<script>
var chat = io.connect('http://localhost') …Run Code Online (Sandbox Code Playgroud) 我有一个Proxy模仿虚拟物体的方法。该代理的 Getter 返回准备好的值。
我发现,如果等待代理,它会导致调用我的代理的“then”属性:
await myProxy
Run Code Online (Sandbox Code Playgroud)
在这种情况下我的代理 getter 应该返回什么?返回代理本身或对自身的承诺是一个好主意吗?
例子在这里:
让我感到困惑的是,如果我是await一个对象,我会得到该对象,但如果我是awaita Proxy,它需要有一个返回自身的“then”属性陷阱。
我已经尝试了闭包并发现了意想不到的行为.有人可以解释为什么这段代码会这样工作吗?
function foo() {
this.a='hello';
return {
aaa:function() {
return a; // this suprises me, how can be here accessed 'a' ?
}
}
}
o=foo();
alert(o.aaa()); // prints 'hello' ! , I expected undefined
Run Code Online (Sandbox Code Playgroud)
我不明白,为什么我总是使用var that=this短语,如果可以直接从内部函数访问函数属性.
jsfiddle https://jsfiddle.net/5co6f707/