daily pastebin goal
18%
SHARE
TWEET

Untitled

a guest Mar 21st, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Transport, { ConnectionParameters }  from 'ceres.provider.node.ws';
  2. import Provider from 'ceres.provider';
  3. import * as Protocol from '../protocol/protocol.chat';
  4.  
  5. class ChatServer {
  6.     //Create transport
  7.     private transport: Transport = new Transport(new ConnectionParameters({
  8.         port: 3005
  9.     }));
  10.     private provider: Provider;
  11.     private users: { [key: string]: string } = {};
  12.  
  13.     constructor() {
  14.         // Create provider
  15.         this.provider = new Provider(this.transport);
  16.         // Listen request GetUsers
  17.         this.provider.listenRequest(
  18.             Protocol.Requests.GetUsers,         // Reference to request, which we would like to process
  19.             this.onGetUsersRequest.bind(this)   // Our handler for request
  20.         );
  21.         // Listen request AddUser
  22.         this.provider.listenRequest(
  23.             Protocol.Requests.AddUser,        
  24.             this.onAddUserRequest.bind(this)
  25.         );
  26.         // Listen connect/disconnect events (when client connects/disconnects)
  27.         this.provider.on(Provider.Events.connected, this.onNewClientConnected.bind(this));
  28.         this.provider.on(Provider.Events.disconnected, this.onNewClientDisconnected.bind(this));
  29.     }
  30.  
  31.     private onGetUsersRequest(
  32.         demand: Protocol.Requests.GetUsers,
  33.         clientId: string,
  34.         callback: (error: Error | null, results: Protocol.Responses.UsersList) => any
  35.     ) {
  36.         const users: Protocol.User[] = Object.keys(this.users).map((nickname: string) => {
  37.             return new Protocol.User({ nickname: nickname });
  38.         });
  39.         callback(null, new Protocol.Responses.UsersList({ users: users }));
  40.     }
  41.  
  42.     private onAddUserRequest(
  43.         demand: Protocol.Requests.AddUser,
  44.         clientId: string,
  45.         callback: (error: Error | null, results: Protocol.Responses.AddUserResult) => any
  46.     ) {
  47.         if (this.users[demand.user.nickname] !== void 0) {
  48.             return callback(null, new Protocol.Responses.AddUserResult({ error: 'user already exist' }));
  49.         }
  50.         // Add user to list
  51.         this.users[demand.user.nickname] = clientId;
  52.         // Send response
  53.         callback(null, new Protocol.Responses.AddUserResult({ }));
  54.         // Broadcast actual users list
  55.         this.broadcastUsersList();
  56.     }
  57.  
  58.     private onNewClientConnected(clientId: string) {
  59.         console.log(`New client ${clientId} is connected`);
  60.     }
  61.  
  62.     private onNewClientDisconnected(clientId: string) {
  63.         // Remove user
  64.         Object.keys(this.users).forEach((nickname: string) => {
  65.             if (this.users[nickname] === clientId) {
  66.                 delete this.users[nickname];
  67.             }
  68.         });
  69.         // Broadcast actual users list
  70.         this.broadcastUsersList();
  71.     }
  72.  
  73.     private broadcastUsersList() {
  74.         this.provider.emit(new Protocol.Events.UsersListUpdated({
  75.             users: Object.keys(this.users).map((nickname: string) => {
  76.                 return new Protocol.User({ nickname: nickname });
  77.             })
  78.         })).catch((error: Error) => {
  79.             console.log(`Fail to emit event UsersListUpdated due error: ${error.message}`);
  80.         });
  81.     }
  82.  
  83.  }
  84.  
  85. (new ChatServer());
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top