vue-cli使用sockjs即时通信
基于webSocket通信的库主要有 socket.io,SockJS,这次用的是 SockJS。
这里我们使用sockjs-client、stomjs这两个模块,要实现webSocket通信,需要后台配合,也使用相应的模块。
1、sockjs-client
sockjs-client是从SockJS中分离出来的用于客户端使用的通信模块,所以我们就直接来看看SockJS。SockJS是一个浏览器的JavaScript库,它提供了一个类似于网络的对象,SockJS提供了一个连贯的、跨浏览器的JavaScriptAPI,它在浏览器和Web服务器之间创建了一个低延迟、全双工、跨域通信通道。你可能会问,我为什么不直接用原生的WebSocket而要使用SockJS呢?这得益于SockJS的一大特性,一些浏览器中缺少对WebSocket的支持,因此回退选项是必要的,而Spring框架提供了基于SockJS协议的透明的回退选项。SockJS提供了浏览器兼容性,优先使用原生的WebSocket,如果某个浏览器不支持WebSocket,SockJS会自动降级为轮询。
2、stomjs
STOMP(Simple Text-Orientated Messaging Protocol) 面向消息的简单文本协议,WebSocket是一个消息架构,不强制使用任何特定的消息协议,它依赖于应用层解释消息的含义。与HTTP不同,WebSocket是处在TCP上非常薄的一层,会将字节流转化为文本/二进制消息,因此,对于实际应用来说,WebSocket的通信形式层级过低,因此可以在 WebSocket 之上使用STOMP协议,来为浏览器 和 server间的通信增加适当的消息语义。
STOMP与WebSocket 的关系:
HTTP协议解决了web浏览器发起请求以及web服务器响应请求的细节,假设HTTP协议不存在,只能使用TCP套接字来编写web应用,你可能认为这是一件疯狂的事情
直接使用WebSocket(SockJS)就很类似于使用TCP套接字来编写web应用,因为没有高层协议,就需要我们定义应用间发送消息的语义,还需要确保连接的两端都能遵循这些语义;
同HTTP在TCP套接字上添加请求-响应模型层一样,STOMP在WebSocket之上提供了一个基于帧的线路格式层,用来定义消息语义.
3、代码实现:
先安装 sockjs-client 和 stompjs
npm install sockjs-client
npm install stompjs
简单代码:
<script>
import SockJS from 'sockjs-client'
import Stomp from 'stompjs'
import { getLiveUrlApi, getLiveEventDetailApi } from '@/apis'
import { mapGetters } from 'vuex'
import { picklist, isWinxin, isMobile, initWxShare } from '@/utils'
import Emoji from './emoji'
export default {
data () {
return {
stompClient: '',
timer: '',
player: null,
pkl: picklist,
danmukuList: [],
barrageInfo: {
text: ''
},
total: ,
eventInfo: {
endTime: '',
speakers: [{}]
},
emojiShow: false,
noticeShow: false,
sharehref: '',
isWeixinBrowser: isWinxin(),
isMobileBrowser: isMobile()
}
},
computed: {
...mapGetters(['token', 'userInfo', 'isSys', 'permissions']),
isAdmin () {
// sys、活动管理员、已报名且在直播的普通用户
return this.isSys || (this.eventInfo.ticketStatus === && this.eventInfo.liveStatus === ) || this.permissions.includes('event')
}
},
components: {
Emoji
},
methods: {
getEvent () {
getLiveEventDetailApi(this.$route.params.id).then(res => {
if (res.status === ) {
this.eventInfo = res.data
if (this.isWeixinBrowser) {
this.initShare()
}
if (this.isAdmin) {
this.fetchData()
}
}
})
},
fetchData () {
getLiveUrlApi(this.$route.params.id).then(res => {
if (res.status === ) {
this.aliPlay(res.data)
}
})
},
// 阿里云视频直播
aliPlay (source) {
let _videoDom = document.getElementById('aliVedio')
let _ch = _videoDom.clientHeight / -
let _cw = _videoDom.clientWidth / -
this.player = new Aliplayer({
id: 'aliVedio',
width: '100%',
source: source,
autoplay: true,
isLive: true,
rePlay: false,
playsinline: true,
preload: true,
controlBarVisibility: 'hover',
useH5Prism: true,
enableStashBufferForFlv: false,
stashInitialSizeForFlv: ,
skinLayout: [
{
'name': 'bigPlayButton',
'align': 'blabs',
'x': _cw,
'y': _ch
},
{
'name': 'controlBar',
'align': 'blabs',
'x': ,
'y': ,
'children': [
{
'name': 'liveDisplay',
'align': 'tlabs',
'x': ,
'y':
},
{
'name': 'fullScreenButton',
'align': 'tr',
'x': ,
'y':
},
{
'name': 'volume',
'align': 'tr',
'x': ,
'y':
}
]
}
],
components: [{
name: 'AliplayerDanmuComponent',
type: AliPlayerComponent.AliplayerDanmuComponent,
args: [this.danmukuList]
}]
}, function (player) {
player._switchLevel =
})
},
// 连接后台
connection () {
let that = this
// 建立连接对象
let sockUrl = '/api/event-websocket?token=' + this.token.substring() + '&eventId=' + this.$route.params.id
let socket = new SockJS(sockUrl)
// 获取STOMP子协议的客户端对象
this.stompClient = Stomp.over(socket)
// 定义客户端的认证信息,按需求配置
let headers = {
Authorization: ''
}
// 向服务器发起websocket连接
this.stompClient.connect(headers, (res) => {
// 订阅服务端提供的某个topic
this.stompClient.subscribe('/topic/event/' + this.$route.params.id, (frame) => {
that.addBarage(JSON.parse(frame.body))
})
that.sentFirst()
}, (err) => {
console.log('失败:' + err)
})
this.stompClient.debug = null
},
// 断开连接
disconnect () {
if (this.stompClient) {
this.stompClient.disconnect()
}
},
// 初始化websocket
initWebSocket () {
this.connection()
let that = this
// 断开重连机制,尝试发送消息,捕获异常发生时重连
this.timer = setInterval(() => {
try {
that.stompClient.send('connect-test')
} catch (err) {
console.log('断线了: ' + err)
that.connection()
}
}, )
},
// 用户加入发送弹幕
keyDown (e) {
if (e.keyCode === ) {
if (e.preventDefault) {
e.preventDefault()
} else {
window.event.returnValue = false
}
this.sentBarrage()
}
},
sentBarrage () {
this.emojiShow = false
if (this.barrageInfo.text === '' || this.barrageInfo.text === '\n') {
this.barrageInfo.text = ''
return false
}
let that = this
this.barrageInfo.eventId = this.eventInfo.id
this.barrageInfo.name = this.userInfo.account
this.barrageInfo.role = this.userInfo.roleName
if (this.permissions.length > ) {
this.barrageInfo.role = 'admin'
}
this.barrageInfo.headimgurl = this.userInfo.headimgurl
let reg = /\#[\u4E00-\u9FA5]{,}\;/gi
this.barrageInfo.comment = this.barrageInfo.text
if (reg.test(this.barrageInfo.text)) {
this.barrageInfo.text = this.barrageInfo.text.replace(reg, '')
}
this.stompClient.send(
'/msg',
{},
JSON.stringify(that.barrageInfo)
)
this.barrageInfo.text = ''
},
// 连接建立,首次发送消息
sentFirst () {
let that = this
this.barrageInfo.eventId = this.eventInfo.id
this.barrageInfo.name = this.userInfo.account
this.barrageInfo.role = this.userInfo.roleName
if (this.permissions.length > ) {
this.barrageInfo.role = 'admin'
}
this.barrageInfo.headimgurl = this.userInfo.headimgurl
this.stompClient.send(
'/msg',
{},
JSON.stringify(that.barrageInfo)
)
},
// 添加弹幕内容
addBarage (content) {
// 推送直播状态修改页面
if (content.live) {
if (this.isSys || this.permissions.includes('event')) {
return
}
this.getEvent()
}
if (content.onlineCount) {
this.total = content.onlineCount
}
let _obj = {
'mode': ,
'stime':
}
content = Object.assign(_obj, content)
this.danmukuList.push(content)
this.$nextTick(() => {
let barrage = document.getElementById('barrage')
barrage.scrollTop = barrage.scrollHeight
})
},
// 将匹配结果替换表情图片
emotion (res) {
let word = res.replace(/\#|\;/gi, '')
const list = [
'微笑', '咧嘴笑', '破涕为笑', '汗', '眯眼', '晕', '害羞', '放电',
'亲亲', '得意', '惊恐', '眼泪', '酷', '色', '吐舌', '害羞笑',
'赞', '同意', '拍手', '鼓掌', '红唇', '冷', '夜晚', '少儿不宜',
'强壮', '浪', '太阳', '香蕉', '举杯', '国旗', '心动', '奶牛',
'恶魔', '礼物', '鸡腿', '玫瑰', '西红柿', '茄子', '西瓜', '草莓'
]
let index = list.indexOf(word)
return `<img src="https://cdn.enmotech.com/attach/twemoji/${index}.png" align="middle" width="28px">`
},
setEmoji (arg) {
this.barrageInfo.text += arg
},
// 微信分享
initShare () {
let _obj = {
title: '活动直播:' + this.eventInfo.title,
img: 'https://cs.enmotech.com/image/event/event_1556264441839.png',
desc: '我正在观看' + this.eventInfo.speakers[].name + '的直播:《' + this.eventInfo.title + '》,快来围观吧!'
}
initWxShare(_obj)
}
},
mounted () {
this.sharehref = location.href
this.getEvent()
this.initWebSocket()
},
beforeDestroy () {
if (this.player) {
this.player.dispose()
this.player = null
}
// 页面离开时断开连接,清除定时器
this.disconnect()
clearInterval(this.timer)
}
}
</script>
vue-cli使用sockjs即时通信的更多相关文章
- Java+Netty、Vue+Element-UI实现的即时通信应用 leo-im
之前工作接触了几个开源的IM产品,再加上曾经用Netty实现过几个服务,于是就有了用Netty实现一个IM的想法,于是用业余时间写了一个IM,和喜欢Netty的程序员们分享. 考虑到方便扩展,在服务端 ...
- vue cli使用融云实现聊天
公司有个项目要实现一个聊天功能,需求如下图,略显随意 公司最终选择融云这个吊炸天的即时通信,文档详细的一匹,刚开始看文档感觉很详细实现起来也不麻烦,有很多开源的demo可以在线演示和下载 不过我们的项 ...
- 【原】iOS学习42即时通信之XMPP(1)
1. 即时通信 1> 概述 即时通讯(Instant Messaging)是目前Internet上最为流行的通讯方式,各种各样的即时通讯软件也层出不穷,服务提供商也提供了越来越丰富的通讯服务功能 ...
- 快速上手最新的 Vue CLI 3
翻译:疯狂的技术宅 原文:blog.logrocket.com/getting-sta- 概要:本文将指导你快速上手 Vue CLI 3,包括最新的用户图形界面和即时原型制作功能的使用步骤. 介绍 尤 ...
- Vue CLI 是如何实现的 -- 终端命令行工具篇
Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统,提供了终端命令行工具.零配置脚手架.插件体系.图形化管理界面等.本文暂且只分析项目初始化部分,也就是终端命令行工具的实现. 0. 用法 ...
- java即时通信小例子
学习java一段时间了,今天写来一个即时通信的小例子练手在其过程中也学到了一些知识拿出来和大家分享,请路过的各位大神多多赐教... 好了下面讲一下基本的思路: 首先,编写服务器端的程序,简单点说吧就是 ...
- iOS开发之使用XMPPFramework实现即时通信(三)
你看今天是(三)对吧,前面肯定有(一)和(二),在发表完iOS开发之使用XMPPFramework实现即时通信(一)和iOS开发之使用XMPPFramework实现即时通信(二)后有好多的小伙伴加我Q ...
- iOS开发之使用XMPPFramework实现即时通信(二)
上篇的博客iOS开发之使用XMPPFramework实现即时通信(一)只是本篇的引子,本篇博客就给之前的微信加上即时通讯的功能,主要是对XMPPFramework的使用.本篇博客中用到了Spark做测 ...
- 基于XMPP协议的Android即时通信系
以前做过一个基于XMPP协议的聊天社交软件,总结了一下.发出来. 设计基于开源的XMPP即时通信协议,采用C/S体系结构,通过GPRS无线网络用TCP协议连接到服务器,以架设开源的Openfn'e服务 ...
随机推荐
- android测试--常用控件测试及测试经验(常见)
1.图片选择器 ================测试中遇到的问题记录(除表中记录的)================================================== ①.曾出现,断 ...
- Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第一集之安装VMware】
[Java全技术路线开发]:https://jq.qq.com/?_wv=1027&k=5knQcPc 由于自己已经安装了VMware,本想转载一篇百度经验的教程,但是我对安装过程有些异议 ...
- 061 SparkStream数据接收原理
1.宏观 2.看英文解释过程 ------------------------------------------------------------------------------------- ...
- day65 request对象,以及方法,response对象,render,redirect
这里的都是我们会频繁使用到的,用得多了自然就会了,我们写项目都是少不了这些用法的,所以这就把老师的博客粘过来就好了, Request对象 官方文档 属性 所有的属性应该被认为是只读的,除非另有说明. ...
- zoj 1002 Fire Net 碉堡的最大数量【DFS】
题目链接 题目大意: 假设我们有一个正方形的城市,并且街道是直的.城市的地图是n行n列,每一个单元代表一个街道或者一块墙. 碉堡是一个小城堡,有四个开放的射击口.四个方向是面向北.东.南和西.在每一个 ...
- Yahoo Programming Contest 2019.E.Odd Subrectangles(思路 线性基)
题目链接 \(Description\) 给定一个\(n\times m\)的\(01\)矩阵.求任意选出\(r\)行.\(c\)列(共\(2^{n+m}\)种方案),使得这\(r\)行\(c\)列的 ...
- Python——运算符
Python算术运算符 以下假设变量: a=10,b=20: 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 ...
- mongoose 根据_id更新数据
let photoId = mongoose.Types.ObjectId(`${virtual.productId[0]}`) await model.photo.findByIdAndUpdate ...
- CSS-三角形及其原理
CSS中三角形在网页中比较常见,以前是图片,不过现在基本上都是通过CSS可以完成,实现比较简单,我们可以看一组简单的三角形效果: 其实实现起来比较简单,通过空div然后设置宽高为0,之后可以四周bor ...
- PhotoshopCC 2017安装破解 + cuterman
之前安装了PhotoshopCC 2017版本的软件,但是绿色版的(安装简介.使用方便).但是在随着Adobe公司对设计的不断追求和工具的不断更新,更加强大.更加优秀的设计插件和工具不断出新,最近朋友 ...