Websocket problem

0 favourites
  • 2 posts
  • My question is not about the construct.

    I want to make a simple and (independent of the construct) web chat using websocket (by which two devices connected to the same Wi-Fi can communicate locally)

    I searched on Google.

    I made a local server named server.js.

    And also an html file for the client.

    Through the cmd window and the command:

    node server.js,

    I ran app server and I was able to successfully connect the client to it.

    But I plan to also run the server.js file in the browser (html file).

    But when I run its codes by html.

    gives an error that:

    require ws(web socket) undefined

    I think the requier doesn't run in the browser. How do I solve it?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • your html should use the websocket client and find the WS

    let socket = new WebSocket("ws://yourws.com");

    socket.onopen = function(e) {

    alert("[open] Connection established");

    alert("Sending to server");

    socket.send("My name is John");

    };

    socket.onmessage = function(event) {

    alert(`[message] Data received from server: ${event.data}`);

    };

    socket.onclose = function(event) {

    if (event.wasClean) {

    alert(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);

    } else {

    // e.g. server process killed or network down

    // event.code is usually 1006 in this case

    alert('[close] Connection died');

    }

    };

    socket.onerror = function(error) {

    alert(`[error]`);

    };

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)