soket.io & web

http://socket.io/get-started/chat/

  • 新建一個文件夾 soketWeb ;
  • 在sokertWeb 文件夾內新建一個 package.json 文件;內容如下:
{
"name": "socketWebExample",
"version": "0.0.1",
"description": "it is my first app",
"dependencies": {
"socket.io": "^1.3.5"
}
}
  • 在控制檯中 進到soketWeb 文件夾中 安裝依賴
npm install --save express@4.10.2
  • 新建一個index.js 文件;內容如下:
var app=require('express')();
var http=require('http').Server(app);
app.get('/',function (req,res) {
res.send('<h1>hello world</h1>');
});
http.listen(3000,function(){
console.log('listening on :3000');
});
  • 在控制檯中 啟動服務器;運行命令
node index.js
  • 在瀏覽器中輸入 http://localhost:3000/ 將看到hello world; 表示運行成功;
  • 修改index.js 的代碼,添加如下:
app.get('/',function (req,res) {
//res.send('<h1>hello world</h1>');
res.sendFile(__dirname+'/index.html');
});
  • 新建一個index.html 文件 內容如下:
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
</body>
</html>
  • 在控制檯中 進到soketWeb 文件夾中 安裝依賴
 npm install --save socket.io
  • 再修改index.js 內容如下:
var app=require('express')();
var http=require('http').Server(app);
var io=require('socket.io')(http);
app.get('/',function (req,res) {
//res.send('<h1>hello world</h1>');
res.sendFile(__dirname+'/index.html');
});
io.on('connection',function(socket){
console.log('has a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
http.listen(3000,function(){
console.log('listening on :3000');
});
  • 在index.html 中添加一下的代碼:
    <script>
var socket = io();
</script>
  • 再一次在瀏覽器中運行 http://localhost:3000/ 如圖;

    好 連接成功!!!
  • 又一次修改 index.html 向服務器 發送消息;
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
</script>
  • 修改index.js 服務器這邊的接收到信息後處理;
io.on('connection',function(socket){
socket.on('chat message', function(msg){
console.log('message: ' + msg);
});
});

OK 到這裏就差不多了 ;

可以開始通話了;

  • 再修改index.js 廣播接收到的信息;
var app=require('express')();
var http=require('http').Server(app);
var io=require('socket.io')(http);
app.get('/',function (req,res) {
//res.send('<h1>hello world</h1>');
res.sendFile(__dirname+'/index.html');
});
io.on('connection',function(socket){
console.log('has a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('chat message', function(msg){
io.emit('chat message', msg);
});
});
http.listen(3000,function(){
console.log('listening on :3000');
});
  • 在index.html 中修改 當接收到信息時 ,添加到聊天記錄裏面 顯示處理;
    <script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>

node socket.io web的更多相关文章

  1. Node学习笔记(三):基于socket.io web版你画我猜(二)

    上一篇基础实现的功能是客户端canvas作图,导出dataURL从而实现图片信息推送,下面具体讲下服务端的配置及客户端的配置同步 首先先画一个流程图,讲下大概思路 <canvas id=&quo ...

  2. Socket IO Web实时推送

    1服务器pom.xml引入 <!-- 服务端 --> <dependency> <groupId>com.corundumstudio.socketio</g ...

  3. Load Testing Socket.IO Web Applications and Infrastructure

    转自:https://medium.com/better-programming/load-testing-socket-io-web-applications-and-infrastructure- ...

  4. Node学习笔记(三):基于socket.io web版你画我猜(一)

    经过惨淡的面试,也是知道了自己的不足,刚好最近在学习node,心中便有了做一个web版的你画我猜的想法 首先说下思路,在做准备工作的时候,有两个大概的思路: 1.规定一块div,捕捉鼠标事件,动态生成 ...

  5. 基于Node.js+socket.IO创建的Web聊天室

    这段时间进了一个新的项目组,项目是用Appcan来做一个跨平台的移动运维系统,其中前台和后台之间本来是打算用WebSocket来实现的,但写好了示例后发现android不支持WebSocket,大为受 ...

  6. 使用Node.js的socket.io模块开发实时web程序

    首发:个人博客,更新&纠错&回复 今天的思维漫游如下:从.net的windows程序开发,摸到nodejs的桌面程序开发,又熟悉了一下nodejs,对“异步”的理解有了上上周对操作系统 ...

  7. 使用Node.js+Socket.IO搭建WebSocket实时应用

    Web领域的实时推送技术,也被称作Realtime技术.这种技术要达到的目的是让用户不需要刷新浏览器就可以获得实时更新.它有着广泛的应用场景,比如在线聊天室.在线客服系统.评论系统.WebIM等. W ...

  8. (转)使用Node.js+Socket.IO搭建WebSocket实时应用

    Web领域的实时推送技术,也被称作Realtime技术.这种技术要达到的目的是让用户不需要刷新浏览器就可以获得实时更新.它有着广泛的应用场景,比如在线聊天室.在线客服系统.评论系统.WebIM等. W ...

  9. [Node.js] 基于Socket.IO 的私聊

    原文地址:http://www.moye.me/2015/01/02/node_socket-io/ 引子 最近听到这么一个问题:Socket.IO 怎么实现私聊?换个提法:怎么定位到人(端),或者说 ...

随机推荐

  1. .NET笔试题集(一)

    1.简述 private. protected. public. internal.protected internal 访问修饰符和访问权限 private : 私有成员, 在类的内部才可以访问. ...

  2. C#获取字符首字母

    ///<summary> /// 获取字符首字母 /// </summary> public static string GetPyChar(string c) { if (s ...

  3. jsp无法引入外部.JS或者.CSS文件的有关问题 (转)

    <!-- *************JSP代码******************--> <%@ page language="java" pageEncodin ...

  4. springmvc学习第一天

    一.helloworld的实现 1.加入jar包(加入无关的jar包可能会引起服务器的冲突) commons-logging-1.2.jarjstl.jarspring-aop-4.1.6.RELEA ...

  5. Day9 summary

    昨天又翻出收藏夹里一个叫“谷子粒”的bloghttp://1.guzili.sinaapp.com/?p=128#more-128,链接是博主整理的机器学习方面的热点微博,相当的干货.要说我是从知乎对 ...

  6. H.264 / MPEG-4 Part 10 White Paper-翻译

    1. Introduction Broadcast(广播) television and home entertainment(娱乐) have been revolutionised(彻底改变) b ...

  7. MySQL 数据库实现远程连接

    1,刚开始我使用的是Navicat for MySQL工具连接远程的mysql的数据库. 报错了.报错信息是 Error 1130: Host '192.168.1.80' is not allowe ...

  8. 百度之星热身赛-1001(dfs拓扑排序)

    题意:作为年度优秀魔法学员的奖赏,哈利得到了一台具有魔力的计算机.这台计算机一旦开始处理某个任务,就会一直处理到这个任务结束为止(所以你可以认为它是单线程的).有一天,这台计算机得到了n个任务要处理, ...

  9. Eos持久化实体

    持久化实体 2009-11-30 20:53:38|  分类: 记事本_学习笔记 |  标签: |举报 |字号大中小 订阅     在EOS的实体定义中,如果使用了关联实体方式,主实体的外键属性是不存 ...

  10. 诺基亚XL中Intent.ACTION_VIEW无效的问题

    今天测试播放视频的时候,发现在诺基亚XL机型里不能弹出视频应用列表. 我的代码是: Intent intent = new Intent(Intent.ACTION_VIEW); intent.set ...