Skip to content
This repository has been archived by the owner on Nov 10, 2022. It is now read-only.

Commit

Permalink
fix: fix confusion between SocketIO and SocietIOClient
Browse files Browse the repository at this point in the history
  • Loading branch information
severo committed Jan 23, 2020
1 parent 5644850 commit 851647e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/domain/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ const randomColor = () => checkColor(rnd({ luminosity: 'dark' }))
const MS_UNTIL_EXPIRATION: number = 10 * 60 * 1000

export class User {
private _socketId: SocketIOClient.Socket['id']
private _socketId: SocketIO.Socket['id']
private _name: string
private _color: string
private _updated_at: Date
private _expired_at: Date
private _msUntilExpiration: number

constructor(
socketId: SocketIOClient.Socket['id'],
socketId: SocketIO.Socket['id'],
{ msUntilExpiration = MS_UNTIL_EXPIRATION } = {
msUntilExpiration: MS_UNTIL_EXPIRATION,
}
Expand Down
19 changes: 9 additions & 10 deletions src/socket.io/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import { StateEvent, UsersListEvent } from '../domain/events/toclient'
import { ExportedUser, User } from '../domain'

class Socket {
private users: Map<SocketIOClient.Socket['id'], User> = new Map()
private users: Map<SocketIO.Socket['id'], User> = new Map()
private state = Automerge.init()

constructor(private io: SocketIO.Server, private log = new ConsoleLogger()) {}

public connect() {
this.io
.of('/occupapp-beta')
.on(ConnectEvent.eventName, (socket: SocketIOClient.Socket) => {
.on(ConnectEvent.eventName, (socket: SocketIO.Socket) => {
this.log.info(`Connection from socket ${socket.id}`)

// Note that a new socket (and this socket.id) is created on each
Expand Down Expand Up @@ -130,35 +130,34 @@ class Socket {
ack: UpdateStateAck
) => {
// We let automerge throw if the data is malformed
// By the way, the state is persisted locally to send to the next
// client that will connect
this.state = Automerge.applyChanges(this.state, data)

// The server doesn't send the changes to the other clients
// It's the task of the client that updated the state to send it to
// them. Using websockets, the latency between two clients will be
// lower than adding an intermediate step through the server)
// TODO: Send to the other users in the namespace

this.log.info('State updated')
ack({ updated: true })
}
})
}

private createUser = (id: SocketIOClient.Socket['id']): User => {
private createUser = (id: SocketIO.Socket['id']): User => {
const newUser = new User(id)
this.users.set(id, newUser)
this.log.info(`createUser`, `New user created (client socket ${id})`)
return newUser
}

private removeUser = (id: SocketIOClient.Socket['id']): boolean => {
private removeUser = (id: SocketIO.Socket['id']): boolean => {
const removed: boolean = this.users.delete(id)
if (removed) {
this.log.info(`removeUser`, `User removed (client socket ${id})`)
}
return removed
}

// private emitInternalServerError(socket: SocketIOClient.Socket, error: Error) {
// private emitInternalServerError(socket: SocketIO.Socket, error: Error) {
// let internalServerErrorEvent = new InternalServerErrorEvent(error)
// socket.emit(
// InternalServerErrorEvent.eventName,
Expand All @@ -177,7 +176,7 @@ class Socket {
return [...this.users.values()].map(user => user.export())
}

private emitStateToUser(socket: SocketIOClient.Socket) {
private emitStateToUser(socket: SocketIO.Socket) {
const stateEvent = new StateEvent(this.stateAsJson)
socket.emit(StateEvent.eventName, stateEvent.state)
}
Expand Down

0 comments on commit 851647e

Please sign in to comment.