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. 【bzoj1597- [Usaco2008 Mar]土地购买】斜率优化

    [597][Usaco2008 Mar]土地购买 [题目描述] 有N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000 ...

  2. NGINX: 限制连接的实践 (Defense DDOS)

    参考: [ nginx防止DDOS攻击配置 ] 关于限制用户连接,Nginx 提供的模块: [ ngx_http_limit_req_module ] [ ngx_http_limit_conn_mo ...

  3. #error#学习方法,如何避免初始化错误

    #error#学习方法,如何避免初始化错误.错误来自:本博客的另一篇文章Demo示例程序源代码: ,01-导航实例-QQ空间.xcodeproj - CYLLoginViewController.mD ...

  4. npm install 报node-sass错误

    Node Sass could not -bit with Node.js .x Found bindings for the following environments: - OS X -bit ...

  5. requests上传文件

    """ requests上传文件时,如果文件名是中文,会导致上传失败,参考:https://www.cnblogs.com/liaofeifight/p/5807901. ...

  6. Kuangbin 带你飞-基础计算几何专题 题解

    专题基本全都是模版应用.贴一下模版 平面最近点对 const double INF = 1e16; ; struct Point { int x,y; int type; }; double dist ...

  7. Vue CLI3 关闭热替换后出现的warning

    用vue cli3做项目的时候如果开启了typescript的严格模式,在dev server热替换的时候往往就会打出一大堆warning,严重的影响了编译效率.官方并没有提供关闭warning的ap ...

  8. normalize.css v2.1.2 翻译

    /*! normalize.css v2.1.2 | MIT License | git.io/normalize */ /* /*! 我就是自己看看,然后翻译下下,让大家看看 */ /* ===== ...

  9. POJ 3984 迷宫问题【BFS/路径记录/手写队列】

    迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31428 Accepted: 18000 Description 定义 ...

  10. (寒假开黑gym)2017-2018 ACM-ICPC German Collegiate Programming Contest (GCPC 2017)

    layout: post title: (寒假开黑gym)2017-2018 ACM-ICPC German Collegiate Programming Contest (GCPC 2017) au ...