Building real time app with Socket.io and Node.js
来源:互联网 发布:淘宝发送优惠券给买家 编辑:程序博客网 时间:2024/06/06 00:33
Socket.io is a framework which implement WebSocket in HTML5. 我们可以通过其和Node.js实现实时通信。
var http = require('http').Server(app);var io = require('socket.io')(http);io.on('connection', function(socket){console.log('a user connected');//监听新用户加入socket.on('login', function(obj){//将新加入用户的唯一标识当作socket的名称,后面退出的时候会用到socket.name = obj;//检查在线列表,如果不在里面就加入//向所有客户端广播用户加入//sleep(3000);socket.emit('logout', 'bbbb');console.log('加入了聊天室');});});
socket.send("aa") == socket.emit("message", "aa")
Broadcasting: socket.broadcast.emit('message', msg);
客户端代码示例:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script src="http://127.0.0.1:3000/socket.io/socket.io.js"></script> <script> $(function(){ //var iosocket = io.connect(); var iosocket = io.connect('ws://127.0.0.1:3000'); iosocket.on('connect', function () { $('#incomingChatMessages').append($('<li>Connected</li>')); iosocket.on('message', function(message) { $('#incomingChatMessages').append($('<li></li>').text(message)); }); iosocket.on('disconnect', function() { $('#incomingChatMessages').append('<li>Disconnected</li>'); }); }); $('#outgoingChatMessage').keypress(function(event) { if(event.which == 13) { event.preventDefault(); iosocket.send($('#outgoingChatMessage').val()); $('#incomingChatMessages').append($('<li></li>').text($('#outgoingChatMessage').val())); $('#outgoingChatMessage').val(''); } }); }); </script>
0 0
- Building real time app with Socket.io and Node.js
- Real-Time Applications with Java and CORBA
- Practical Node.js: Building Real-World Scalable Web Apps
- socket.io node.js mysql
- Building and running Node.js for Android
- Building and running Node.js for Android
- Creating a real-time search engine with IndexTank and Heroku
- Real-Time 3D Rendering with DirectX and HLSL
- 使用 Node.js 與 Socket.IO 建立即時性(Realtime)網頁應用程式 App
- ubuntu install node.js socket.io
- Socket.IO 和 Node.js 入门
- node.js+socket.io配置详解
- node.js+socket.io+聊天室源码
- Socket.IO 和 Node.js 聊天程序
- node.js+socket.io私人聊天
- Socket.IO 和 Node.js 入门
- Socket.IO 和 Node.js 聊天程序
- Node.js+socket.io简单在线聊天
- 使用pip来安装和管理第三方Python包(library)
- HowTo: Export C++ classes from a DLL
- 消息摘要算法HmacMD5的实现
- 1257:最少拦截系统
- SSH整合(一)——直接获取ApplicationContext
- Building real time app with Socket.io and Node.js
- 第一层第一题:你要的飞碟在这里
- WEB网站常见的攻击方法总结与原理分析
- 困扰我的【编程】-如果它能让你快乐-继续做!
- ios-下载管理类封装
- 黑马程序员——C语言基础---运算符
- 9.27
- 2015年终总结(上)
- 动态规划之《寻找最大上升序列》