我有一个 node.js-app 在同一台机器上的 8080 端口上运行,具有不同的 channel 。我的 jQuery 站点和我的 .NET 端点之间的通信完美无缺。
我的网站:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>WebSocket-Test</title>
</head>
<body>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
$(function() {
$('button').click(function() {
var socket = io.connect('http://localhost:4000');
socket.on($('#username').val(), function (data) {
console.log(data);
socket.emit('my other event', { my: data });
});
});
});
</script>
<input type="text" id="username" />
<button>connect</button>
</body>
</html>
我的 node.js 服务器:
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(4000);
var count = 0;
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
setInterval(function () {
socket.emit('daniel', { hello: 'Waited two seconds!'});
}, 2000);
socket.emit('daniel', { hello: 'world' });
socket.emit('stefan', { hello: 'world2' });
socket.on('my other event', function (data) {
console.log(data);
});
});
我的问题是,如何通过 node.js 从我的 .NET 后端发出消息?
我的页面加载后,我就有了一个 window.io 对象。什么是最好的方法?只需使用 emmit 和 channel 对 io-object 进行评估,或者我可以将对象或 json-thing 传递到我的 node.js-server 吗?
我的目标是发送事件驱动的消息。当一个新行插入到我的 MSQL-DB 时,应该向 channel 发送一条消息。
最佳答案
您可以做的一件事是在有详细信息更新时简单地 ping Node.js 服务器。您可以直接通过 http/https 执行此操作。
基本上,当 .NET 更新数据库时,它可以使用您要向用户推出的数据包向 node.js 端点发出快速 POST。
关于vb.net - 从 VB.NET 与 node.js 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16572712/