0

I am a Korean developer.

I'm making a game and a game bot now.

Games run on WebGL and gamebots run on node JS.

There is a game git in the gamebot git. Because the game bot git has only scenario and is basically the same as normal game

The problem is, I'm using WebSocket in the code in this game git, but I imported the ws library because Node Js does not support WebSocket.

But,

import WebSocket from 'ws';

This code causes an error in the browser.

What should I do to use the websocket in both Nodejs and Browser with the same code?

Jeckl
  • 1
  • 1
  • 1
    Does this answer your question? [how to use node.js module system on the clientside](https://stackoverflow.com/questions/4944863/how-to-use-node-js-module-system-on-the-clientside) – Justinas Aug 24 '22 at 11:24

1 Answers1

0

You can't use a websocket on the back and the frond end with the same code if only because they do different things. In the browser you don't need to import anything as websockets are supported natively. You can read on it in MDN: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications

Michał Sadowski
  • 1,946
  • 1
  • 11
  • 24
  • I already knew that the browser came with a websocket - but my point is, I don't want to use different code when using a gamegit on a client and when using it on a gamebot. – Jeckl Aug 24 '22 at 11:29
  • Or, you can tell me the library where it is possible. For example, it was previously unavailable in NodeJs using XMLHttpRequest, but now using Axios, both Nodejs and Browser operate in the same code – Jeckl Aug 24 '22 at 11:33