Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable and disable MockSocket at runtime #361

Open
SerkanSipahi opened this issue May 7, 2022 · 2 comments
Open

Enable and disable MockSocket at runtime #361

SerkanSipahi opened this issue May 7, 2022 · 2 comments

Comments

@SerkanSipahi
Copy link

I would like to switch between a mocked socket and a real websocket during development in the frontend. For this reason I am looking for a way to switch the mock socket on and off at runtime.

Unfortunately, I did not find anything in the documentation on how to achieve this. Is it possible to achieve this with the Server.stop(...) method because internally this method takes something out of the networkBridge.removeServer(this.url)? Probably not because the global real websocket is overwritten by the mock. What is the right way here?

Thank you very much in advance.

@SerkanSipahi SerkanSipahi changed the title Enable and disable MockSocket Enable and disable MockSocket at runtime! May 7, 2022
@SerkanSipahi SerkanSipahi changed the title Enable and disable MockSocket at runtime! Enable and disable MockSocket at runtime May 7, 2022
@SerkanSipahi
Copy link
Author

SerkanSipahi commented May 7, 2022

I think I've found what I'm looking for, restoreWebsocket and mockWebsocket can turn on/off the mockSocketServer at runtime.

We should add this to the documentation (README.md). If that is ok, I would create a MR for this.

mockWebsocket (enable)

mockWebsocket() {
const globalObj = globalObject();
this.originalWebSocket = globalObj.WebSocket;
globalObj.WebSocket = WebSocket;
}

restoreWebsocket (disable)

restoreWebsocket() {
const globalObj = globalObject();
if (this.originalWebSocket !== null) {
globalObj.WebSocket = this.originalWebSocket;
}
this.originalWebSocket = null;

@Atrue
Copy link
Collaborator

Atrue commented May 24, 2022

@SerkanSipahi Yep, it is. Go on, you can create a pull request for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants