generated from nim-od/softeer-mono-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #56 from softeerbootcamp4th/fix-crypto
[Fix] 임시 해결 - Error: Dynamic require of "crypto" is not supported
- Loading branch information
Showing
3 changed files
with
95 additions
and
2 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
2 changes: 1 addition & 1 deletion
2
packages/user/src/services/socket.ts → packages/user/src/services/socket/index.ts
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { Socket } from '@softeer/common/utils'; | ||
import { SOCKET_BASE_URL } from 'src/constants/environments.ts'; | ||
import Socket from 'src/services/socket/socket.ts'; | ||
|
||
const socketClient = new Socket(SOCKET_BASE_URL); | ||
export default socketClient; |
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,93 @@ | ||
import { Client, IFrame, IMessage, StompSubscription } from '@stomp/stompjs'; | ||
import SockJS from 'sockjs-client'; | ||
|
||
/** | ||
* 임시로 error 해결 전까지 common 에 존재하는 Socket class 대신 사용 | ||
* Error: Dynamic require of "crypto" is not supported | ||
* */ | ||
export default class Socket { | ||
private client: Client; | ||
|
||
private subscriptions: Map<string, StompSubscription> = new Map(); | ||
|
||
constructor(url: string, token?: string | null) { | ||
this.client = this.setup({ url, token }); | ||
} | ||
|
||
private setup({ url }: { url: string; token?: string | null }): Client { | ||
const stompClient = new Client({ | ||
webSocketFactory: () => new SockJS(`${url}/ws`), | ||
reconnectDelay: 5000, // Reconnect if the connection drops | ||
}); | ||
this.client = stompClient; | ||
return stompClient; | ||
} | ||
|
||
connect(callback?: (isConnected: boolean, options?: IFrame) => void) { | ||
// TODO: 채팅 메시지 리스트 받아오기 | ||
// this.client.onConnect = (options) => callback?.(true, options); | ||
this.client.onConnect = () => callback?.(true); | ||
|
||
this.client.onStompError = (error) => { | ||
console.error('STOMP Error', error); | ||
callback?.(false); | ||
}; | ||
|
||
this.client.activate(); | ||
} | ||
|
||
disconnect() { | ||
this.subscriptions.forEach((subscription) => subscription.unsubscribe()); | ||
this.subscriptions.clear(); | ||
|
||
if (this.client.connected) { | ||
this.client.deactivate(); | ||
} | ||
} | ||
|
||
sendMessages({ destination, body }: { destination: string; body: unknown }) { | ||
if (!this.client.connected) { | ||
this.connect(() => { | ||
this.client.publish({ destination, body: JSON.stringify(body) }); | ||
}); | ||
} else { | ||
this.client.publish({ destination, body: JSON.stringify(body) }); | ||
} | ||
} | ||
|
||
private createSubscription({ | ||
destination, | ||
callback, | ||
}: { | ||
destination: string; | ||
callback: (messageId: string, message: IMessage) => void; | ||
}) { | ||
const subscription = this.client.subscribe(destination, (message: IMessage) => { | ||
const messageId = message.headers['message-id']; | ||
callback(messageId, message); | ||
}); | ||
this.subscriptions.set(destination, subscription); | ||
} | ||
|
||
subscribe({ | ||
destination, | ||
callback, | ||
}: { | ||
destination: string; | ||
callback: (messageId: string, message: IMessage) => void; | ||
}) { | ||
if (this.client.connected) { | ||
this.createSubscription({ destination, callback }); | ||
} else { | ||
this.connect(() => this.createSubscription({ destination, callback })); | ||
} | ||
} | ||
|
||
unsubscribe(destination: string) { | ||
const subscription = this.subscriptions.get(destination); | ||
if (subscription) { | ||
subscription.unsubscribe(); | ||
this.subscriptions.delete(destination); | ||
} | ||
} | ||
} |