2013-05-15 44 views
3

我有一個node.js-app在端口8080上的同一臺機器上運行不同的通道。我的jQuery網站和我的.NET端點之間的通信非常完美。與VB.NET中的node.js進行通信

我的網站:

<!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後端emmit的消息?

我的頁面加載後,我確實有一個window.io對象。什麼是最好的方法?只需在emmit和channel上對io-object進行評估,或者我可以將對象或json-thing傳遞給我的node.js-server?

我的目標是發送一個事件驅動的消息。當一個新行插入到我的MSQL-DB中時,應發送一條消息給這些通道。

+0

只是順便說一句。 StackOverflow允許您直接發佈代碼,包含代碼突出顯示。 (這使測試更容易,因爲您可以複製和粘貼所要求的內容。) – TheHippo

+0

感謝您的提示,我編輯了我的帖子。 – webprogrammer

+1

您是否計劃讓.NET調查數據庫以發佈事件?插入是從其他來源發生的嗎?此外,這[問題](http://stackoverflow.com/q/12867731/945456)可能會幫助... –

回答

1

你可以做的一件事情就是當有細節更新時,只需ping Node.js服務器即可。你可以通過直接的http/https來做到這一點。

基本上,當.NET更新數據庫時,它可以使用要向用戶推出的數據包快速POST POST到node.js端點。