connect into a websocket

32 Views Asked by At

I've been trying to connect to my websocket but I got a problem can you help me out? this is my code:

const websocket = require('ws');
const PORT = 3000; 

const ws = new websocket.Server({ port: PORT});

ws.on('connection', function connection(wss) {
    client.send("connected");
    wss.on('message', function incoming(message) {
        ws.clients.forEach(function each(client) {
            client.send(message);
        });
    });
});

I'm trying to connect but nothing works:

      ws = new WebSocket('ws://thekillerhood.net/chat');
      ws.onopen = () => {
        console.log('Connection opened!');
      }
      ws.onmessage = ({ data }) => showMessage(data);
      ws.onclose = function() {
        ws = null;
      }

This is my url: https://thekillerhood.net/chat/

I've been trying to connect with difference ways but nothing seems to work (I'm learning how to make a websocket)

0

There are 0 best solutions below