12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- import { playAudio } from '@/utils/audio.js'
- const _WEBSOCKET = {
- //是否打开连接
- isOpen: false,
- //连接socket
- connectSocket(url,successFunc = null, errorFunc = null) {
- try {
- //连接socket
- uni.connectSocket({
- url,
- success() {
- console.log('websocket连接成功!')
- }
- })
- //监听socket连接
- uni.onSocketOpen((res) => {
- this.isOpen = true
- console.log('WebSocket连接已打开!')
- if (successFunc) {
- successFunc(res)
- }
- })
- //监听socket连接失败
- uni.onSocketError((res) => {
- this.isOpen = false
- console.log('WebSocket连接打开失败,请检查!')
- if (errorFunc) {
- errorFunc(res)
- }
- })
- //监听收到消息
- uni.onSocketMessage((res) => {
- console.log('收到服务器内容:' + res.data)
-
- playAudio()
- uni.showToast({
- title: '您有新的单子',
- duration: 2000
- });
-
- // uni.redirectTo({
- // url:"/pages/index/index?tips=1"
- // })
- })
- //监听socket关闭
- uni.onSocketClose((res) => {
- console.log('WebSocket 已关闭!')
- this.isOpen = false
- })
- } catch (error) {
- console.log('err:' + error)
- }
- },
- //发送消息
- sendMessage(msg = '', successFunc = null, errorFunc = null) {
- if (!this.isOpen || !msg) {
- if (errorFunc) {
- errorFunc('未传消息内容或连接未打开!')
- }
- return
- }
- uni.sendSocketMessage({
- data: msg,
- success(res) {
- console.log('消息发送成功!')
- if (successFunc) {
- successFunc(res)
- }
- },
- fail(err) {
- console.log('消息发送失败!')
- if (errorFunc) {
- errorFunc(err)
- }
- }
- })
- },
- //关闭连接
- closeSocket() {
- if (!this.isOpen) {
- return
- }
- //关闭socket连接
- uni.closeSocket()
- }
- }
- export default _WEBSOCKET
|