You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

client-pusher.ts 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import Pusher from 'pusher-js'
  2. import * as PusherTypes from 'pusher-js'
  3. import state from 'state/state'
  4. import { Shape } from 'types'
  5. class RoomClient {
  6. room: string
  7. pusher: Pusher
  8. channel: PusherTypes.PresenceChannel
  9. lastCursorEventTime = 0
  10. id: string
  11. constructor() {
  12. // Create pusher instance and bind events
  13. this.pusher = new Pusher('5dc87c88b8684bda655a', {
  14. cluster: 'eu',
  15. authEndpoint: 'http://localhost:3000/api/pusher-auth',
  16. })
  17. this.pusher.connection.bind('connecting', () =>
  18. state.send('RT_CHANGED_STATUS', { status: 'connecting' })
  19. )
  20. this.pusher.connection.bind('connected', () =>
  21. state.send('RT_CHANGED_STATUS', { status: 'connected' })
  22. )
  23. this.pusher.connection.bind('unavailable', () =>
  24. state.send('RT_CHANGED_STATUS', { status: 'unavailable' })
  25. )
  26. this.pusher.connection.bind('failed', () => {
  27. state.send('RT_CHANGED_STATUS', { status: 'failed' })
  28. })
  29. this.pusher.connection.bind('disconnected', () => {
  30. state.send('RT_CHANGED_STATUS', { status: 'disconnected' })
  31. })
  32. }
  33. connect(roomId: string) {
  34. this.room = 'presence-' + roomId
  35. // Subscribe to channel
  36. this.channel = this.pusher.subscribe(
  37. this.room
  38. ) as PusherTypes.PresenceChannel
  39. this.channel.bind('pusher:subscription_error', (err: string) => {
  40. console.warn(err)
  41. state.send('RT_CHANGED_STATUS', { status: 'subscription-error' })
  42. })
  43. this.channel.bind('pusher:subscription_succeeded', () => {
  44. const me = this.channel.members.me
  45. const userId = me.id
  46. this.id = userId
  47. state.send('RT_CHANGED_STATUS', { status: 'subscribed' })
  48. })
  49. this.channel.bind(
  50. 'created_shape',
  51. (payload: { id: string; pageId: string; shape: Shape }) => {
  52. if (payload.id === this.id) return
  53. state.send('RT_CREATED_SHAPE', payload)
  54. }
  55. )
  56. this.channel.bind(
  57. 'deleted_shape',
  58. (payload: { id: string; pageId: string; shape: Shape }) => {
  59. if (payload.id === this.id) return
  60. state.send('RT_DELETED_SHAPE', payload)
  61. }
  62. )
  63. this.channel.bind(
  64. 'edited_shape',
  65. (payload: { id: string; pageId: string; change: Partial<Shape> }) => {
  66. if (payload.id === this.id) return
  67. state.send('RT_EDITED_SHAPE', payload)
  68. }
  69. )
  70. this.channel.bind(
  71. 'client-moved-cursor',
  72. (payload: { id: string; pageId: string; point: number[] }) => {
  73. if (payload.id === this.id) return
  74. state.send('RT_MOVED_CURSOR', payload)
  75. }
  76. )
  77. }
  78. disconnect() {
  79. this.pusher.unsubscribe(this.room)
  80. }
  81. reconnect() {
  82. this.pusher.subscribe(this.room)
  83. }
  84. moveCursor(pageId: string, point: number[]) {
  85. if (!this.channel) return
  86. const now = Date.now()
  87. if (now - this.lastCursorEventTime > 200) {
  88. this.lastCursorEventTime = now
  89. this.channel?.trigger('client-moved-cursor', {
  90. id: this.id,
  91. pageId,
  92. point,
  93. })
  94. }
  95. }
  96. }
  97. export default new RoomClient()