`
hongtoushizi
  • 浏览: 374893 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

mqtt和socket.io实现socket互通

    博客分类:
  • mqtt
阅读更多
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path')
, socketio = require('socket.io')
, mqtt = require('mqtt');
 
// Express server
var app = express();
 
// Http server
var server = http.createServer(app);
 
// Socket.io
var io = socketio.listen(server);
 
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
 
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
 
app.get('/', routes.index);
 
io.sockets.on('connection', function(socket) {
var client = mqtt.createClient();
 
socket.on('publish', function(topic, message) {
client.publish(topic, message);
});
socket.on('subscribe', function (topic) {
client.subscribe(topic);
});
client.on('message', function(topic, message) {
socket.emit('message', topic, message);
});
});
 

server.listen(app.get('port'));

 

 

一篇好的git共享代码: https://github.com/adamvr/bearded-dangerzone/blob/master/app.js

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics