Azure移动服务-Socket.IO集成
我在创建Azure移动服务自定义脚本时遇到问题,我想使用Socket.IO Node.js模块,但我不知道如何编辑Azure移动服务路由以能够访问/Socket.IO/1 执行后,此代码socket.io已启动,但客户端无法从浏览器访问URL端点,请帮助我,提前谢谢,我的电子邮件是:stepanic。matija@gmail.com 我的代码是: in/api/notifyAzure移动服务-Socket.IO集成,azure,socket.io,azure-mobile-services,Azure,Socket.io,Azure Mobile Services,我在创建Azure移动服务自定义脚本时遇到问题,我想使用Socket.IO Node.js模块,但我不知道如何编辑Azure移动服务路由以能够访问/Socket.IO/1 执行后,此代码socket.io已启动,但客户端无法从浏览器访问URL端点,请帮助我,提前谢谢,我的电子邮件是:stepanic。matija@gmail.com 我的代码是: in/api/notify exports.register = function (api) { api.get('socket.io',
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' });
}
Socket.io当前不受支持 在中,可能会使其工作,但您需要在移动服务启动脚本中使用提供的应用程序对象执行此代码
您还需要更新其中的路由,以便在移动服务路由之前获取路由。@stepanic,您可以尝试将Socket.io客户端捆绑为静态文件。以下是我们在帆船上的操作方法,以供参考: 发件人:
//“io”是一个全局变量。
//`io.socket`将自动连接,但它还没有准备好(想想jQuery中的$(document.ready())。
//幸运的是,这个库提供了一个抽象来避免这个问题。
//在“io”准备就绪之前发出的请求将在套接字连接时自动排队和重播。
//要禁用此行为或配置其他内容,可以在“io”上设置属性。
//在自动连接行为开始之前,您有一个事件循环来更改'io'设置。
io.socket.get('/hello',函数serverresponsed(body,sailsResponseObject){
//body==sailsResponseObject.body
log('Sails响应:',body);
log('with headers:',sailsResponseObject.headers);
console.log('and with status code:',sailsResponseObject.statusCode);
});
使用启动脚本扩展添加了对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();
}
详情请参阅:
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();
}