使用SockJS和StompJS实现WebSocket订阅服务

一、首先用npm安装 socketJS 和 stompJS

npm install sockjs-client
npm install stompjs

二、在页面中引入这2个js

import SockJS from 'sockjs-client'
import Stomp from 'stompjs'

三、初始化websocket连接,定义一个对象(socketClient)接收订阅服务的实例化

// 初始化ws连接
initSocketConnection () {
    const _url = 'Your Websocket Url'
    const _socket = new SockJS(_url)
    this.socketClient = Stomp.over(_socket)
	// 向服务器发起websocket连接并发送CONNECT帧
	this.socketClient.connect(
		{ login: '', passcode: '' },
		// 连接成功的回调函数
		function connectCallback (success) {
			console.log('webSocket连接成功:', success)
		},
		// 连接失败时的回调函数
		function errorCallBack (error) {
			console.log('webSocket连接失败:', error)
		}
	)
}

四、开始订阅(如需多个订阅频道,可以用一个变量monitorList)

// 点击开始订阅按钮
onStartMonitor () {
	// 多个订阅频道时找出当前激活的频道
    const _activeRoom = this.monitorList.find(item => item.name === this.activeMonitorTabName)
    _activeRoom.isPaused = false
    this.startChatRoom()
},

// 点击暂停订阅按钮
onPauseMonitor () {
	// 多个订阅频道时找出当前激活的频道
    const _activeRoom = this.monitorList.find(item => item.name === this.activeMonitorTabName)
    _activeRoom.isPaused = true
    this.pauseChatRoom()
},

// 执行开始订阅
startChatRoom () {
	// 找出当前激活的频道
    const _activeRoom = this.monitorList.find(item => item.name === this.activeMonitorTabName)
    // 自己定义查询的参数
    const params = {
        'chatRoomId': _activeRoom.chatRoomId,
    }
    // 转换格式为base64,也可以不转,看你的服务器需求
    const query = window.btoa(JSON.stringify(params))
    // 为当前频道增加订阅服务
    _activeRoom.subscribe = this.socketClient.subscribe(`/client/roomChat/?jsonStr=${query}`, (message) => {
    	// 可以看看message里面的东西,取出想要的即可
        const _result = JSON.parse(message.body)
        if (_result.data.chatLogs && _result.data.chatLogs.length > 0) {
            const _chatData = _result.data.chatLogs
            _activeRoom.tableData = _chatData.concat(_activeRoom.tableData)
        }
    })
},

// 暂停订阅
pauseChatRoom (name) {
    let _activeRoom = this.monitorList.find(item => item.name === this.activeMonitorTabName)
    if (name) {
        _activeRoom = this.monitorList.find(item => item.name === name)
    }
    if (_activeRoom.subscribe) {
        _activeRoom.subscribe.unsubscribe()
    }
},

// 销毁ws连接
disconnect () {
    if (this.socketClient) {
        this.socketClient.disconnect()
    }
},

// vue生命周期,离开或关闭页面时销毁ws连接
beforeDestroy () {
	this.disconnect()
},

五、增加订阅频道功能

// 增加频道
onAddMonitor () {
    if (this.monitorList.length >= 10) {
        return this.$Message.error('最多只能添加10个频道')
    }
    // 使用一个计数器来标记唯一的频道id
    this.counter++
    const _obj = { ...this.monitorTemplate }
    _obj.id = new Date().getTime()
    _obj.name = _obj.name + '_' + _obj.id
    _obj.chatRoomId = this.counter + '_1'
    this.monitorList.push(_obj)
    if (this.monitorList.length === 1) {
        this.activeMonitorTabName = this.monitorList[0].name
    }
},

六、所需要的data数据

data() {
	return {
		monitorList: [],
		monitorTemplate: {
			id: null,
            label: '频道',
            name: 'monitorTab',
            chatRoomId: '',
            isPaused: true,
            subscribe: null,
            tableData: [],
		},
		activeMonitorTabName: '',
		socketClient: null,
	}
}

相关网站:
sockjs

https://github.com/sockjs/sockjs-node

stomp-websocket

https://github.com/jmesnil/stomp-websocket

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
实现WebSocket-FLV直播服务,您可以结合使用JavaCV和Spring Boot来完成。 首先,JavaCV是一个基于Java的开源计算机视觉和机器学习库,它提供了许多常见的计算机视觉和机器学习功能,如图像处理、视频处理、特征提取等。 而Spring Boot则是一个用于创建独立的、生产级别的Spring应用程序的框架,它简化了Spring应用程序的配置和部署过程,使开发者能够快速构建可靠的Java应用程序。 要实现WebSocket-FLV直播服务,您可以按照以下步骤进行: 1. 首先,使用Spring Boot创建一个新的项目。可以使用Spring Initializr(https://start.spring.io/)来初始化一个基本的Spring Boot项目。 2. 然后,添加JavaCV的依赖。您可以在项目的pom.xml文件中添加以下依赖来引入JavaCV: ```xml <dependency> <groupId>org.bytedeco</groupId> <artifactId>javacv</artifactId> <version>1.5.3</version> </dependency> ``` 3. 接下来,实现WebSocket连接和FLV视频流的处理。使用Spring Boot提供的WebSocket支持来建立和管理WebSocket连接,并使用JavaCV从视频源生成FLV格式的视频流。 4. 在WebSocket处理程序中,可以使用JavaCV的视频处理功能来读取视频源,然后将读取到的每一帧编码为FLV格式的视频流。可以使用JavaCV提供的FFmpegFrameGrabber类来读取视频源,并使用JavaCV提供的FFmpegFrameRecorder类来编码视频流为FLV格式。 5. 最后,将生成的FLV视频流发送给连接的WebSocket客户端。使用Spring Boot提供的WebSocket发送功能将FLV视频流发送给与WebSocket连接建立的客户端。 以上就是使用JavaCV和Spring Boot实现WebSocket-FLV直播服务的基本步骤。您可以根据具体需求来调整和扩展这个基本实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值