Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mesa integration #117

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions components/Player/Viewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
}
},
computed: {
...mapGetters(['ws', 'userId', 'controllerId', 'portal', 'janusId', 'janusIp', 'apertureWs', 'apertureToken', 'viewerMuted', 'viewerVolume']),
...mapGetters(['mesa', 'userId', 'controllerId', 'portal', 'janusId', 'janusIp', 'apertureWs', 'apertureToken', 'viewerMuted', 'viewerVolume']),

hasControl() {
return this.controllerId === this.userId
Expand Down Expand Up @@ -453,11 +453,14 @@
this.emitEvent({ scrollUp: deltaY > 0 }, 'MOUSE_SCROLL')
},

emitEvent(d, t) {
if (!this.ws || !this.hasControl || this.ws.readyState !== this.ws.OPEN)
emitEvent(data, type) {
if (!this.mesa || !this.hasControl || this.mesa.readyState !== WebSocket.OPEN)
return

this.ws.send(JSON.stringify({ op: 0, d, t }))
this.mesa.send({
opcode: 0,
data, type
})
},

calculatePos(event) {
Expand Down
2 changes: 2 additions & 0 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import brand from './brand/config'

const borealis = process.env.BOREALIS_REPOSITORY ? process.env.BOREALIS_REPOSITORY : '@cryb/borealis'

export const strict = false

const script = []
if (brand.ga_tracking_id)
script.push({
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"cookieparser": "^0.1.0",
"date-fns": "^2.5.1",
"dotenv": "^8.2.0",
"mesa-js-client": "^1.4.4",
"node-emoji": "^1.10.0",
"nuxt": "^2.8.1",
"nuxt-client-init-module": "^0.1.4",
Expand Down
227 changes: 89 additions & 138 deletions store/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { parse } from 'cookieparser'
import cookies from 'browser-cookies'

import Mesa from 'mesa-js-client'

const isProduction = () => process.env.NODE_ENV === 'production'

export const getters = {
Expand Down Expand Up @@ -50,7 +52,7 @@ export const getters = {
viewerMuted: ({ viewerMuted }) => viewerMuted,
viewerVolume: ({ viewerVolume }) => (viewerVolume/100),

ws: ({ ws }) => ws
mesa: ({ mesa }) => mesa
}

const initialState = () => ({
Expand Down Expand Up @@ -79,10 +81,7 @@ const initialState = () => ({
viewerMuted: false,
viewerVolume: 30,

ws: null,
wsHeartbeat: null,
wsReconnect: null,
wsReconnectInterval: 5000
mesa: null
})

export const state = () => initialState()
Expand Down Expand Up @@ -310,10 +309,16 @@ export const mutations = {
* Typing
*/
setTypingStatus(state, typing) {
if (!state.ws) return
if (state.ws.readyState !== state.ws.OPEN) return
if (!state.mesa && !state.mesa.ws)
return
else if (state.mesa.ws.readyState !== WebSocket.OPEN)
return

state.ws.send(JSON.stringify({ op: 0, d: { typing }, t: 'TYPING_UPDATE' }))
state.mesa.send({
opcode: 0,
data: { typing },
type: 'TYPING_UPDATE'
})
},

updateTypingStatus(state, { typing, u: userId }) {
Expand All @@ -329,12 +334,12 @@ export const mutations = {
* Presence
*/
updatePresence(state, { u: userId, presence }) {
if (userId === state.userId) return
if (userId === state.userId) return

if(presence === 'online' && state.onlineUsers.indexOf(userId) === -1)
state.onlineUsers.push(userId)
else
state.onlineUsers.splice(state.onlineUsers.indexOf(userId), 1)
if(presence === 'online' && state.onlineUsers.indexOf(userId) === -1)
state.onlineUsers.push(userId)
else
state.onlineUsers.splice(state.onlineUsers.indexOf(userId), 1)
},

/**
Expand All @@ -360,149 +365,95 @@ export const mutations = {
* WebSocket
*/
setupWebSocket(state) {
if (!state.token) return

const { token } = state, ws = new WebSocket(process.env.WS_URL)
console.log(state.token)
if (!state.token)
return

ws.onerror = () => this.commit('setupReconnect')
const { token } = state, mesa = new Mesa(process.env.WS_URL, { autoConnect: false })

ws.onmessage = ({ data }) => {
let json
mesa.onConnected = async () => {
console.log('connected')

try {
json = JSON.parse(data)
} catch(error) {
console.error(error)

return console.error(error)
}

const { op, d, t } = json

if (op !== 11 && !isProduction())
console.log(op, d, t)

if (op === 0) {
if (t.split('_')[0] === 'PORTAL')
return this.commit('updatePortal', d)
switch(t) {
// ROOM
case 'CONTROLLER_UPDATE':
this.commit('updateController', d)
break
case 'QUEUE_UPDATE':
this.commit('updateQueueStatus', d)
break
case'JANUS_CONFIG':
this.commit('updateJanus', d)
break
case 'APERTURE_CONFIG':
this.commit('updateAperture', d)
console.log("Aperture config received.")
break
case 'ROOM_DESTROY':
this.commit('handleRoom', null)
this.app.router.push('/home')
break
case 'INVITE_UPDATE':
this.commit('handleInvite', d)
break
// USER
case 'USER_JOIN':
this.commit('handleUserJoin', d)
break
case 'USER_UPDATE':
this.commit('handleUser', d)
break
case 'USER_LEAVE':
this.commit('handleUserLeave', d)
break
case 'OWNER_UPDATE':
this.commit('handleOwnerUpdate', d)
break
case 'PRESENCE_UPDATE':
this.commit('updatePresence', d)
break
// MESSAGE
case 'MESSAGE_CREATE':
this.commit('pushMessage', d)
break
case 'MESSAGE_DESTROY':
this.commit('pullMessage', d.id)
break
case 'TYPING_UPDATE':
this.commit('updateTypingStatus', d)
break
}
} else if (op === 10) {
const { c_heartbeat_interval, c_reconnect_interval } = d

this.commit('setupHeartbeat', c_heartbeat_interval)

if (state.wsReconnect) this.commit('invalidateReconnect')
state.wsReconnectInterval = c_reconnect_interval

ws.send(JSON.stringify({ op: 2, d: { token } }))
}
await mesa.authenticate({ token })
}

state.ws = ws
mesa.onMessage = (op, d, t) => {
if (op === 0) {
if (t.split('_')[0] === 'PORTAL')
return this.commit('updatePortal', d)

switch(t) {
// ROOM
case 'CONTROLLER_UPDATE':
this.commit('updateController', d)
break
case 'QUEUE_UPDATE':
this.commit('updateQueueStatus', d)
break
case'JANUS_CONFIG':
this.commit('updateJanus', d)
break
case 'APERTURE_CONFIG':
this.commit('updateAperture', d)
console.log("Aperture config received.")
break
case 'ROOM_DESTROY':
this.commit('handleRoom', null)
this.app.router.push('/home')
break
case 'INVITE_UPDATE':
this.commit('handleInvite', d)
break
// USER
case 'USER_JOIN':
this.commit('handleUserJoin', d)
break
case 'USER_UPDATE':
this.commit('handleUser', d)
break
case 'USER_LEAVE':
this.commit('handleUserLeave', d)
break
case 'OWNER_UPDATE':
this.commit('handleOwnerUpdate', d)
break
case 'PRESENCE_UPDATE':
this.commit('updatePresence', d)
break
// MESSAGE
case 'MESSAGE_CREATE':
this.commit('pushMessage', d)
break
case 'MESSAGE_DESTROY':
this.commit('pullMessage', d.id)
break
case 'TYPING_UPDATE':
this.commit('updateTypingStatus', d)
break
}
}
}

mesa.connect()

state.mesa = mesa
},

disconnectWebSocket(state) {
if (state.ws) {
state.ws.close(1000)
state.ws = null
if (state.mesa) {
state.mesa.disconnect(1000)
state.mesa = null
}

if (state.portal)
state.portal = null

if (state.controllerId)
state.controllerId = null

if (state.wsHeartbeat)
this.commit('invalidateHeartbeat')

if (state.wsReconnect)
this.commit('invalidateReconnect')
},

setupHeartbeat(state, interval) {
state.wsHeartbeat = setInterval(() => {
if (!state.ws) return this.commit('invalidateHeartbeat')
if (state.ws.readyState !== state.ws.OPEN) return this.commit('invalidateHeartbeat')

state.ws.send(JSON.stringify({ op: 1, d: {} }))
}, interval)
},

invalidateHeartbeat(state) {
if (!state.wsHeartbeat) return

clearInterval(state.wsHeartbeat)
state.wsHeartbeat = null
},

setupReconnect(state) {
state.wsReconnect = setInterval(() => {
if (state.ws)
if (state.ws.readyState === state.ws.OPEN)
return this.commit('invalidateReconnect')

this.commit('setupWebSocket')
}, state.wsReconnectInterval)
},

invalidateReconnect(state) {
if (!state.wsReconnect) return

clearInterval(state.wsReconnect)
state.wsReconnect = null
},

logout(_state) {
if (_state.ws)
if (_state.mesa)
this.commit('disconnectWebSocket')

const state = initialState(), SAFE_KEYS = []
Expand Down
Loading