1、先安装npm和node

2、安装socket.io

npm install socket.io

3、html

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ssocket</title>
<script src="http://localhost:8088/socket.io/socket.io.js"></script>
</head> <body>
<div></div>
<input type="text"/>
<button>button</button>
<script type="text/javascript">
var input = document.getElementsByTagName("input")[0];
var button = document.getElementsByTagName("button")[0];
var div = document.getElementsByTagName("div")[0];
var socket = io.connect('http://localhost:8088');
var data;
socket.on('news', function (data) {
div.innerHTML=data.my;
console.log(data);
});
button.onclick=function(){
socket.emit('my other event', { my: input.value });
};
</script> </body>
</html>

4、js

/**
* Created by zcwl123 on 2017/5/9.
*/
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs') app.listen(8088);
io.set('log level', 1);//将socket.io中的debug信息关闭 function handler (req, res) {
fs.readFile(__dirname + '/index.html',function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data);
});
} io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
io.sockets.emit('news', data);
console.log(data);
console.log(1);
});
});

5、启动server.js

node server.js

6、其余api参考

nodeJs+socket.io的更多相关文章

  1. nodejs + socket.io + redis 新手上路

    最近要更新网站架构了,决定转入 nodejs + socket.io + redis 方式. 战斗刚开始: 网上的文章太松散,我根据各个网友的分享进行整理 ,让大家可以方便上手. 进入node.js之 ...

  2. NodeJS + Socket.io搭建聊天服务器

    第一步:安装node git clone https://github.com/joyent/node.git cd node git checkout v0.10.33-release ./conf ...

  3. nodejs+socket.io即时聊天实例

    在这之前你应该先安装好 Node.js,安装过程不再讲解 首先在你的电脑上创建一个新目录,姑且命名为 chat,然后在该目录创建两个文件,分别是 app.js 和 index.html. app.js ...

  4. nodejs socket.io初探

    1.安装socket.io npm install socket.io 2.创建服务端代码server.js var app = require('http').createServer(handle ...

  5. nodeJS+socket.io传递消息

    服务器端 安装express,socket.io npm install express --save-dev npm install socket.io --save app.js const ex ...

  6. NodeJS + Socket.io聊天服务器连接数达到1024后就连不上了

    如果是亚马逊的Engine Yard服务器,解决办法为: 1.查看端口占用情况,找到nodejs进程号,例如我这里是8000端口 lsof -i:8000  找到pid 例如为 8213 2.设置no ...

  7. Socket.io+Notification实现浏览器消息推送

    前言 socket.io: 包含对websocket的封装,可实现服务端和客户端之前的通信.详情见官网(虽然是英文文档,但还是通俗易懂).Notification: Html5新特性,用于浏览器的桌面 ...

  8. 在web浏览器上显示室内温度(nodeJs+arduino+socket.io)

    上次的nodejs操作arduino入门篇中实现了如何连接arduino.这次我们来实现通过arduino测量室内温度并在浏览器上显示出来. [所需材料] 硬件:LM35温度传感器,arduino u ...

  9. NodeJS+Express+Socket.io的一个简单例子

    关键字:NodeJS,Express,Socket.io. OS:Windows 8.1 with update pro. 1.安装NodeJS:http://nodejs.org/. 2.初始化一个 ...

随机推荐

  1. 【Foreign】字符串匹配 [KMP]

    字符串匹配 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 3 3 6 3 1 2 1 2 ...

  2. bzoj 2245 费用流

    比较裸 源点连人,每个人连自己的工作,工作连汇,然后因为人的费用是 分度的,且是随工作数非降的,所以我们拆边,源点连到每个人s+1条边 容量是每段的件数,费用是愤怒 /**************** ...

  3. concurrent

    from concurrent.futures import ThreadPoolExecutor

  4. python2.7字典转换成json时中文字符串变成unicode的问题:

    参考:http://blog.csdn.net/u014431852/article/details/53058951 编码问题: python2.7字典转换成json时中文字符串变成unicode的 ...

  5. html——零散知识点

    1.form表单中的button  form表单中,正常应该提交数据的是type="submit"   2. html5的文件读取方法FileReader()     3.inpu ...

  6. springmvc Converter

    以下,来自于Springmvc指南第二版,第93页. Spring的Converter是可以将一种类型转为另一种类型. 例如用户输入的date类型可能有多种格式. 比如:在controller中接收一 ...

  7. MYSQL使用外键进行优化

    #转载请联系 假如你是京东的数据库管理员,你现在管理着这样一个数据库. mysql> select * from goods; +----+--------------------------- ...

  8. hdu 1224(动态规划 DAG上的最长路)

    Free DIY Tour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  9. Android 使用WebView控件展示SVG图

    1.添加布局界面代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xm ...

  10. java反射(基本知识)

    在java中反射降低了模块间的依赖性这个过程称解耦---高内聚,低耦合 在java中,万物皆对象,则将字节码看成一个对象,将一个方法看成一个对象..... 反射--剖析类,分析类的字节码,产生对象的字 ...