-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from one-piece-team1/feat-sockets
Feat sockets
- Loading branch information
Showing
17 changed files
with
336 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ COPY ./package.json ./ | |
RUN npm install | ||
COPY . . | ||
RUN npm run build | ||
EXPOSE 7070 8080 | ||
EXPOSE 84 7070 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import Kafka from 'node-rdkafka'; | ||
import { ChatMessageRoutingService } from '../handlers/chat.handler'; | ||
import { config } from '../../config'; | ||
|
||
@Injectable() | ||
export class ChatConsumerService { | ||
private readonly logger: Logger = new Logger('ChatConsumerService'); | ||
private readonly consumer = new Kafka.KafkaConsumer( | ||
{ | ||
'bootstrap.servers': config.EVENT_STORE_SETTINGS.bootstrapServers, | ||
'group.id': config.EVENT_STORE_SETTINGS.chat.groupId, | ||
'enable.auto.commit': true, | ||
}, | ||
{ | ||
'auto.offset.reset': 'earliest', | ||
}, | ||
); | ||
|
||
constructor( | ||
private readonly chatMessageRoutingService: ChatMessageRoutingService, | ||
) { | ||
this.init(); | ||
} | ||
|
||
/** | ||
* @description Init func | ||
*/ | ||
init() { | ||
this.consumer | ||
.on('ready', () => { | ||
this.consumer.subscribe([config.EVENT_STORE_SETTINGS.topics.chatTopic]); | ||
setInterval(() => { | ||
this.consumer.consume(config.EVENT_STORE_SETTINGS.poolOptions.max); | ||
}, 1000); | ||
}) | ||
.on('data', data => { | ||
this.chatMessageRoutingService.register(data); | ||
this.consumer.commit(); | ||
}) | ||
.on('event.error', err => { | ||
this.logger.error(err.message, '', 'Event_Error'); | ||
}) | ||
.on('rebalance.error', err => { | ||
this.logger.error(err.message, '', 'Reblanace_Error'); | ||
}); | ||
|
||
this.consumer.connect({}, (err, data) => { | ||
if (err) { | ||
this.logger.error(err.message, '', 'ConsumerConnectError'); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { | ||
Injectable, | ||
InternalServerErrorException, | ||
Logger, | ||
} from '@nestjs/common'; | ||
import Kafka from 'node-rdkafka'; | ||
import { ChatSocketService } from '../sockets/chat.service'; | ||
import * as EChatRoom from '../sockets/enums'; | ||
import * as IChatRoom from '../sockets/interfaces'; | ||
|
||
@Injectable() | ||
export class ChatMessageRoutingService { | ||
private readonly logger: Logger = new Logger('ChatMessageRoutingService'); | ||
|
||
constructor(private readonly chatSocketService: ChatSocketService) {} | ||
|
||
public register(kafkaMessage: Kafka.Message) { | ||
if (!kafkaMessage) | ||
throw new InternalServerErrorException('Non message is being proecssed'); | ||
const event: IChatRoom.IAggregateResponse< | ||
EChatRoom.EChatRoomSocketEvent, | ||
IChatRoom.IEventData | ||
> = JSON.parse(kafkaMessage.value.toString()); | ||
return this.handler(event); | ||
} | ||
|
||
protected handler( | ||
event: IChatRoom.IAggregateResponse< | ||
EChatRoom.EChatRoomSocketEvent, | ||
IChatRoom.IEventData | ||
>, | ||
) { | ||
switch (event.type) { | ||
case EChatRoom.EChatRoomSocketEvent.CREATECHATROOM: | ||
return this.chatSocketService.sendNewChatRoom( | ||
event.type, | ||
event.data as IChatRoom.IChatRoomEntity, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import * as Express from 'express'; | ||
import WebSocket from 'ws'; | ||
import * as url from 'url'; | ||
import { config } from '../../config'; | ||
|
||
@Injectable() | ||
export class ChatSocketGateway { | ||
public wss: WebSocket.Server; | ||
private readonly logger: Logger = new Logger('ChatSocketGateway'); | ||
|
||
constructor() { | ||
this.init(); | ||
} | ||
|
||
/** | ||
* @description Init func | ||
*/ | ||
public init() { | ||
this.wss = new WebSocket.Server({ port: config.WSPORT, path: '/chats' }); | ||
this.wss.on('connection', (ws: WebSocket, req: Express.Request) => { | ||
ws.on('message', (message: string) => { | ||
this.logger.log('Messaging is on'); | ||
}); | ||
this.logger.log('Connecting ws success'); | ||
const qs: url.UrlWithParsedQuery = url.parse(req.url, true); | ||
ws['uid'] = qs.query.userIds; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { ChatSocketGateway } from './chat.gateway'; | ||
import * as EChatRoom from './enums'; | ||
import * as IChatRoom from './interfaces'; | ||
|
||
@Injectable() | ||
export class ChatSocketService { | ||
private readonly logger: Logger = new Logger('ChatSocketService'); | ||
|
||
constructor(private readonly chatSocketGateway: ChatSocketGateway) {} | ||
|
||
/** | ||
* @description Verify Identity | ||
* @public | ||
* @param {IChatRoom.ISocketWithIdentity} client | ||
* @param {IChatRoom.IChatRoomEntity} chatRoom | ||
* @returns {boolean} | ||
*/ | ||
protected isRightClient( | ||
client: IChatRoom.ISocketWithIdentity, | ||
chatRoom: IChatRoom.IChatRoomEntity, | ||
): boolean { | ||
let isClient = false; | ||
chatRoom.participateId.userIds.forEach(user => { | ||
if (user.id === client.uid) { | ||
isClient = true; | ||
} | ||
}); | ||
return isClient; | ||
} | ||
|
||
/** | ||
* @description send new chat room | ||
* @public | ||
* @param {EChatRoom.EChatRoomSocketEvent} type | ||
* @param {IChatRoom.IChatRoomEntity} chatRoomEvent | ||
* @returns {void} | ||
*/ | ||
public sendNewChatRoom( | ||
type: EChatRoom.EChatRoomSocketEvent, | ||
chatRoomEvent: IChatRoom.IChatRoomEntity, | ||
): void { | ||
this.chatSocketGateway.wss.clients.forEach( | ||
(client: IChatRoom.ISocketWithIdentity) => { | ||
const isClient = this.isRightClient(client, chatRoomEvent); | ||
if (isClient) { | ||
client.send(JSON.stringify({ type, data: chatRoomEvent }), err => { | ||
if (err) { | ||
this.logger.error(err.message, '', 'SendNewChatRoom'); | ||
} | ||
}); | ||
} | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export enum EChatRoomType { | ||
'PUBLIC' = 'public', | ||
'PRIVATE' = 'private', | ||
'GROUP' = 'group', | ||
} | ||
|
||
export enum EChatRoomSocketEvent { | ||
'CREATECHATROOM' = 'createchatroom', | ||
'UPDATECHATROOM' = 'updatechatroom', | ||
'DELETECHATROOM' = 'deletechatroom', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './chat-room.enum'; | ||
export * from './user.enum'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export enum EUserRole { | ||
'TRIAL' = 'trial', | ||
'USER' = 'user', | ||
'VIP1' = 'vip1', | ||
'VIP2' = 'vip2', | ||
'ADMIN' = 'admin', | ||
} | ||
|
||
export enum EUserGender { | ||
'MALE' = 'male', | ||
'FEMALE' = 'female', | ||
} |
Oops, something went wrong.