2014-02-26 40 views
2

我有創建Azure移動服務自定義腳本的問題,我想使用Socket.IO Node.js模塊,但我不知道如何編輯Azure移動服務路線才能夠訪問/socket.io/1Azure移動服務 - Socket.IO集成

執行此代碼後,socket.io啓動,但客戶端無法從瀏覽器訪問URL端點,請幫助我,預先感謝您,我的電子郵件是:stepanic.matija @ gmail.com

我的代碼是:

在/ API /通知

exports.register = function (api) { 

    api.get('socket.io',getSocketIO); 

}; 

function getSocketIO(req,res) 
{ 
var app = require('express')() 
    , server = require('http').createServer(app) 
    , io = require('socket.io').listen(server); 

server.listen(80); 

app.get('/', function (req, res) { 
    res.sendfile(__dirname + '/index.html'); 
}); 

io.sockets.on('connection', function (socket) { 
    socket.emit('news', { hello: 'world' }); 
    socket.on('my other event', function (data) { 
    console.log(data); 
    }); 
}); 

res.send(statusCodes.OK, { message : '23 Hello World!', bla: 'bla2' }); 
} 

回答

0

@stepanic,您可以嘗試將Socket.io客戶端捆綁爲靜態文件。下面是我們如何做到這一點的帆以供參考:

the docs

<!-- .... --> 
    </body> 
    <script type="text/javascript" src="./path/to/bower_components/sails.io.js"></script> 
    <script type="text/javascript"> 

    // `io` is available as a global. 
    // `io.socket` will connect automatically, but it is not ready yet (think of $(document).ready() from jQuery). 
    // Fortunately, this library provides an abstraction to avoid this issue. 
    // Requests you make before `io` is ready will be queued and replayed automatically when the socket connects. 
    // To disable this behavior or configure other things, you can set properties on `io`. 
    // You have one cycle of the event loop to change `io` settings before the auto-connection behavior starts. 

    io.socket.get('/hello', function serverResponded (body, sailsResponseObject) { 

     // body === sailsResponseObject.body 
     console.log('Sails responded with: ', body); 
     console.log('with headers: ', sailsResponseObject.headers); 
     console.log('and with status code: ', sailsResponseObject.statusCode); 
    }); 
    </script> 
</html> 
1

支持Socket.IO一直使用的啓動腳本擴展

var path = require('path'); 

exports.startup = function (context, done) { 
    var io = require('socket.io')(context.app.server); 
    io.on('connection', function(socket){ 
     socket.on('chat message', function(msg){ 
     io.emit('chat message', msg); 
     }); 
    }); 

     context.app.get('/public/chat.html', function(req, res) { 
     res.sendfile(path.resolve(__dirname, '../public/chat.html')); 
    }); 
    done(); 
} 

詳見添加:http://azure.microsoft.com/blog/2014/08/26/how-to-use-socket-io-with-azure-mobile-service-node-backend/