How to use the network/middleware/rpc.RpcRealm.Multicast function in network

To help you get started, we’ve selected a few network examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github samuelmaddock / metastream / packages / metastream-app / src / lobby / actions / chat.ts View on Github external
}
        : undefined,
      content: text,
      timestamp: Date.now()
    })
  )

  // Clear user typing immediately if we received a message from them
  const typingTimeout = userId && userTypingTimeouts[userId]
  if (typingTimeout) {
    clearTimeout(typingTimeout)
    userTypingTimeouts[userId!] = undefined
    dispatch(clearTyping(userId!))
  }
}
export const multi_broadcastChat = rpc('broadcastChat', RpcRealm.Multicast, broadcastChat)

const rpcAddChat = (text: string): RpcThunk => (dispatch, getState, context) => {
  text = text.trim()
  if (text.length === 0) return false

  if (text.length > CHAT_MAX_MESSAGE_LENGTH) {
    text = text.substr(0, CHAT_MAX_MESSAGE_LENGTH)
  }

  const userId = context.client.id.toString()
  dispatch(multi_broadcastChat(text, userId))
  return true
}
const server_addChat = rpc('rpcAddChat', RpcRealm.Server, rpcAddChat)

export const sendChat = (text: string): AppThunkAction => {
github samuelmaddock / metastream / packages / metastream-app / src / lobby / actions / users.ts View on Github external
const username = getUserName(getState(), userId)
  const content = translateEscaped('userLeft', { userId, username })
  dispatch(addChat({ content, html: true, timestamp: Date.now() }))
}
export const multi_userLeft = rpc('userLeft', RpcRealm.Multicast, userLeft)

const userNameChanged = (userId: string, prevName: string): RpcThunk => (
  dispatch,
  getState,
  context
) => {
  const name = getUserName(getState(), userId)
  const content = translateEscaped('userNameChanged', { userId, name, prevName })
  dispatch(addChat({ content, html: true, timestamp: Date.now() }))
}
export const multi_userNameChanged = rpc('userNameChanged', RpcRealm.Multicast, userNameChanged)

const kickClient = (reason: NetworkDisconnectReason): RpcThunk => (
  dispatch,
  getState,
  { server }
) => {
  console.debug(`Received kick with reason: '${reason}'`)
  dispatch(setDisconnectReason(reason))
  server.close()
}
export const client_kick = rpc('kickClient', RpcRealm.Client, kickClient)

const kickUser = (targetId: string): RpcThunk => (dispatch, getState, context) => {
  const state = getState()
  const requesterId = context.client.id.toString()
github samuelmaddock / metastream / packages / metastream-app / src / lobby / actions / users.ts View on Github external
)
export const setUserRole = actionCreator<{ userId: string; role: UserRole; enabled: boolean }>(
  'SET_USER_ROLE'
)
export const updateUser = actionCreator<{ userId: string } & Partial>('UPDATE_USER')

const userJoined = (userId: string): RpcThunk => (dispatch, getState, context) => {
  if (localUserId() === userId) {
    return
  }

  const username = getUserName(getState(), userId)
  const content = translateEscaped('userJoined', { userId, username })
  dispatch(addChat({ content, html: true, timestamp: Date.now() }))
}
export const multi_userJoined = rpc('userJoined', RpcRealm.Multicast, userJoined)

const userLeft = (userId: string): RpcThunk => (dispatch, getState, context) => {
  const username = getUserName(getState(), userId)
  const content = translateEscaped('userLeft', { userId, username })
  dispatch(addChat({ content, html: true, timestamp: Date.now() }))
}
export const multi_userLeft = rpc('userLeft', RpcRealm.Multicast, userLeft)

const userNameChanged = (userId: string, prevName: string): RpcThunk => (
  dispatch,
  getState,
  context
) => {
  const name = getUserName(getState(), userId)
  const content = translateEscaped('userNameChanged', { userId, name, prevName })
  dispatch(addChat({ content, html: true, timestamp: Date.now() }))
github samuelmaddock / metastream / packages / metastream-app / src / lobby / actions / mediaPlayer.ts View on Github external
if (getNumUsers(getState()) === 1) return

  const media = getMediaById(getState(), mediaId)
  if (!media) return

  const content = translateEscaped('noticeNowPlaying', {
    userId: media.ownerId,
    username: media.ownerName,
    mediaId: media.id,
    mediaTitle: media.title
  })
  dispatch(addChat({ content, html: true, timestamp: Date.now() }))
}
export const multi_announceMediaChange = rpc(
  'announceMediaChange',
  RpcRealm.Multicast,
  announceMediaChange
)

export const enqueueMedia = (media: IMediaItem, index?: number): AppThunkAction => {
  return (dispatch, getState): boolean => {
    const state = getState()
    const current = getCurrentMedia(state)
    let queued: boolean

    if (current) {
      dispatch(queueMedia({ media, index }))
      queued = true
    } else {
      dispatch(setMedia(media))
      dispatch(updatePlaybackTimer())
      dispatch(multi_announceMediaChange(media.id))
github samuelmaddock / metastream / packages / metastream-app / src / lobby / actions / chat.ts View on Github external
dispatch(addChat({ content, timestamp: Date.now() }))
    }
  }
}

const broadcastTyping = (userId: string): RpcThunk => dispatch => {
  if (userId === localUserId()) return
  dispatch(recordTyping(userId))

  let timeout = userTypingTimeouts[userId]
  if (timeout) clearTimeout(timeout)
  userTypingTimeouts[userId] = setTimeout(() => {
    dispatch(clearTyping(userId))
  }, TYPING_DURATION) as any
}
const multi_broadcastTyping = rpc('broadcastTyping', RpcRealm.Multicast, broadcastTyping)

const rpcNotifyTyping = (): RpcThunk => (dispatch, getState, context) => {
  const userId = context.client.id.toString()
  dispatch(multi_broadcastTyping(userId))
}
export const server_notifyTyping = rpc('rpcNotifyTyping', RpcRealm.Server, rpcNotifyTyping)