nodeJS+socket.io传递消息
服务器端
安装express,socket.io
npm install express --save-dev
npm install socket.io --save
app.js
const express = require('express');
const app = express();
var server = require('http').createServer(app).listen(8083);
var io = require('socket.io')(server);
io.on('connection', function(socket) {
// 想客户端传递消息
socket.emit('news',{hello:'hello'})
// 接收并处理客户端的hi事件
socket.on('start', function(data) {
// ....
})
// 断开事件
socket.on('disconnect', function(data) {
console.log('断开通话');
//socket.emit('c_leave','离开');
//socket.broadcast用于向整个网络广播(除自己之外)
//socket.broadcast.emit('c_leave','某某人离开了')
})
});
客户端
安装socket.io。
注意:这里不能npm install socket.io。这个是服务器端的插件应用。不是客户端。
直接在index.html中引入socke.io。
这样就可以用io这个变量了
<script src="https://cdn.bootcss.com/socket.io/2.2.0/socket.io.dev.js"></script>
$('#submit').click(function(){
var socket = io.connect('http://localhost:8080');
socket.on('news', function (data) {
alert(data.hello);
});
socket.emit('start',1)
// 可以用close()断开连接。服务器端disconnect会监听到
socket.close();
})
相关文档api
https://github.com/socketio/socket.io-client/blob/master/docs/API.md
nodeJS+socket.io传递消息的更多相关文章
- nodejs + socket.io + redis 新手上路
最近要更新网站架构了,决定转入 nodejs + socket.io + redis 方式. 战斗刚开始: 网上的文章太松散,我根据各个网友的分享进行整理 ,让大家可以方便上手. 进入node.js之 ...
- NodeJS + Socket.io搭建聊天服务器
第一步:安装node git clone https://github.com/joyent/node.git cd node git checkout v0.10.33-release ./conf ...
- 利用socket.io实现消息实时推送
最近在写的项目中存在着社交模块,需要实现这样的一个功能:当发生了用户被点赞.评论.关注等操作时,需要由服务器向用户实时地推送一条消息.最终完成的项目地址为:socket-message-push,这里 ...
- nodejs socket.io初探
1.安装socket.io npm install socket.io 2.创建服务端代码server.js var app = require('http').createServer(handle ...
- nodejs+socket.io即时聊天实例
在这之前你应该先安装好 Node.js,安装过程不再讲解 首先在你的电脑上创建一个新目录,姑且命名为 chat,然后在该目录创建两个文件,分别是 app.js 和 index.html. app.js ...
- nodeJs+socket.io
1.先安装npm和node 2.安装socket.io npm install socket.io 3.html <!DOCTYPE html> <html lang="e ...
- Socket.io发送消息含义
仅作收藏:转自博客园 若相忆; // send to current request socket client socket.emit('message', "this is a test ...
- NodeJS + Socket.io聊天服务器连接数达到1024后就连不上了
如果是亚马逊的Engine Yard服务器,解决办法为: 1.查看端口占用情况,找到nodejs进程号,例如我这里是8000端口 lsof -i:8000 找到pid 例如为 8213 2.设置no ...
- Socket.io+Notification实现浏览器消息推送
前言 socket.io: 包含对websocket的封装,可实现服务端和客户端之前的通信.详情见官网(虽然是英文文档,但还是通俗易懂).Notification: Html5新特性,用于浏览器的桌面 ...
随机推荐
- 分享关于浏览器对象 history对象
window.history.forward() == window.history.go(-1) //返回下一页 window.history.back() == window.history.go ...
- asp.net 关于cookie的操作
一.无子键或单级cookie 读写(1).写入: 第一种 HttpCookie cookie=new HttpCookie("User"); cookie.Value=" ...
- 计算机科学书籍推荐和CSS、js书籍推荐
计算机科学:<深入理解计算机系统>,这是基础知识 JavaScript:JavaScript高级程序设计:大名鼎鼎的红宝书 <精通CSS:高级Web标准解决方案>:因为我觉CS ...
- php获取csv数据无乱码
<?php //获取csv数据 function csvencode($file){ if(!is_file($file['tmp_name'])){ ...
- vue踩坑-This relative module was not found
在使用vue.js的日期选择插件 的时候,报错如下 This relative module was not found: * ../calendar.vue in ./node_modules/ba ...
- HRBUST 1818 石子合并问题--直线版
石子合并问题--直线版 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HRBUST. Original ...
- python程序转exe程序之一——cx_Freeze
原始网页 : http://keliang.blog.51cto.com/3359430/661884 本人用的64位系统,一开始装了32位的cx_freeze,结果貌似无法自动找到本地的python ...
- python编程练习
python练习之冒泡排序: python代码: #coding=utf-8 if __name__=="__main__": arr=[3,2,1,7,11,4,5,8] pri ...
- Vue v-bind的使用
1.src <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...
- vue组件中 IS 用法
//html <link rel="stylesheet" href="http://www.jq22.com/demo/animate-141106223642/ ...