在构建项目之前必须在自己目录中增加socketio模块,如图所示:命令为npm install socketio
app.js
1 /** 2 * Created by caner on 2016/9/9. 3 * socket.io 4 */ 5 var http = require('http'); 6 var fs = require('fs'); 7 var server = http.createServer(function(req,res){ 8 fs.readFile('./index.html',function(error,data){ 9 res.writeHead(200,{'Content-Type':'text/html'}); 10 res.end(data,'utf-8'); 11 }) 12 }).listen(3000,'127.0.0.1'); 13 console.log('server connection....'); 14 15 var io = require('socket.io').listen(server); 16 io.sockets.on('connection',function(socket){ 17 socket.on('message', function (data) { 18 socket.broadcast.emit('push message',data); 19 console.log(data); 20 }) 21 })
服务端页面:
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title>服务端页面发送数据</title> 6 </head> 7 <body> 8 <form action="http://127.0.0.1:3000"> 9 <textarea id="message" cols="30" rows="5"></textarea> 10 <input type="button" value="发送" οnclick="t_push()"> 11 </form> 12 <script src="http://127.0.0.1:3000/socket.io/socket.io.js"></script> 13 <script> 14 var socket = io.connect('http://127.0.0.1:3000'); 15 var message = document.getElementById('message'); 16 function t_push(){ 17 socket.emit('message',{text:message.value}); 18 document.getElementById('message').value = ''; 19 } 20 21 </script> 22 </body> 23 </html>
View Code
客户端页面
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title>客户端页面</title> 6 <script src="http://127.0.0.1:3000/socket.io/socket.io.js"></script> 7 </head> 8 <body> 9 <textarea type="text" id="message"> 10 11 </textarea> 12 <input type="button" value="清除" οnclick="f_delete()"> 13 <script> 14 var socket = io.connect('http://127.0.0.1:3000'); 15 socket.on('push message',function(data){ 16 document.getElementById('message').value=data.text; 17 }); 18 function f_delete(){ 19 document.getElementById('message').value=''; 20 } 21 </script> 22 23 </body> 24 </html>
自测成功
var redis = require("redis");
var client = redis.createClient(5555, "1227.0.0.1");
client.AUTH("123456")
var http=require('http')
var server=http.createServer()
server.on('request',function(request,response){
response.end('end')
}).listen(3000)
var sub = function(c) {
var c = c || 'TICK';
client.subscribe(c, function(e) {
console.log('subscribe channel : ' + c);
});
}
sub();
console.log('服务启动成功')
var io = require('socket.io')(server);
io.on('connection', function(socket) {
client.on('message', function(error, msg) {
socket.emit('msgReceived', msg);
});
})
function getRedisData() {
//客户端连接redis成功后执行回调
client.on("ready", function () {
//订阅消息
client.subscribe("TickAPI");
console.log("订阅成功。。。");
});
client.on("error", function (error) {
console.log("Redis Error " + error);
});
//监听订阅成功事件
client.on("subscribe", function (channel, count) {
console.log("client subscribed to " + channel + "," + count + "total subscriptions");
});
//收到消息后执行回调,message是redis发布的消息
client.on("message", function (channel, message) {
console.log(message);
//dealWithMsg(message);
});
//监听取消订阅事件
client.on("unsubscribe", function (channel, count) {
console.log("client unsubscribed from" + channel + ", " + count + " total subscriptions")
});
}
//function dealWithMsg(message) {
//按照message查询内容
//client1.zscore("z", message, function (err, reply) {
// console.log(message + "的内容是:" + reply);
//});
//}
//getRedisData();