mock-socket icon indicating copy to clipboard operation
mock-socket copied to clipboard

Enable and disable MockSocket at runtime

Open SerkanSipahi opened this issue 3 years ago • 2 comments

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 avatar May 07 '22 10:05 SerkanSipahi

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)

https://github.com/thoov/mock-socket/blob/54ba9ced41f424397552879d937da99aa56fface/src/server.js#L46-L51

restoreWebsocket (disable)

https://github.com/thoov/mock-socket/blob/54ba9ced41f424397552879d937da99aa56fface/src/server.js#L56-L63

SerkanSipahi avatar May 07 '22 11:05 SerkanSipahi

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

Atrue avatar May 24 '22 13:05 Atrue