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. [BZOJ2946] [Poi2000]公共串解题报告|后缀数组

    给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 单词个数<=5,每个单词长度<=2000     尽管最近在学的是SAM...但是看到这个题还是忍不住想写SA... (其实是不 ...

  2. bootstrap row 行间距

    <div class="clearfix" style="margin-bottom: 10px;"></div>清除浮动加个margi ...

  3. django返回二进制图片

    @login_required def down_img(request, path): content = Storage().download(path) from django.http imp ...

  4. algorithm ch6 priority queque

    堆数据结构的一个重要用处就是:最为高效的优先级队列.优先级队列分为最大优先级队列和最小优先级队列,其中最大优先级队列的一个应用实在一台分时计算机上进行作业的调度.当用堆来实现优先级队列时,需要在队中的 ...

  5. 百度面试题——top K算法

    需求 从一亿个数据中,找出其中最小的10个数. 分析 最笨的方法就是将这一亿个数据,按从小到大进行排序,然后取前10个.这样的话,即使使用时间复杂度为nlogn的快排或堆排,由于元素会频繁的移动,效率 ...

  6. 1、使用Xcode修改iOS项目工程名和路径名

    http://blog.sina.com.cn/s/blog_a42013280101blxo.html 对,好:错,改正. ------ 前言 系统 10.7 狮子 开发平台 xcode 4.5.2 ...

  7. syntax error near unexpected token `then'问题的解决

    http://blog.csdn.net/gongmin856/article/details/7690917 #!/bin/bash #if program test echo 'a:' read ...

  8. 1.kafka的介绍

    kafka是一种高可用,高吞吐量,基于zookeeper协调的分布式发布订阅消息系统. 消息中间件:生产者和消费者 举个例子: 生产者:做馒头,消费者:吃馒头,数据流:馒头 如果消费者宕机了,吃不下去 ...

  9. tkinter之事件绑定

  10. a标签实现不跳转点击

    <a class="tiao" href="./index.php"></a> JS实现无跳转a标签 <script type=& ...