Giter VIP home page Giter VIP logo

Comments (4)

Atrue avatar Atrue commented on June 3, 2024

I suppose you need to figure out how to work with your test framework. Your tests are not defined properly. I hope it can help you https://mochajs.org/#asynchronous-code

If you think there is an issue in the mock-socket library please create a minimal code snippet. Please also attach the output with the current and expected behaviour. Ideally, this snippet should not have any external library.

from mock-socket.

JJ avatar JJ commented on June 3, 2024

Thanks for the suggestion. Here's the minimal example without any test code (which was minimal anyway)

import { readFileSync } from "fs";
import { WebSocket, Server } from "mock-socket";
import { AIS_API_URL } from "../lib/test-mock.js";

const server = new Server(AIS_API_URL);

let count = 0;
function callback(message) {
  count++;
  if (count === 5) {
    socket.close();
  }
  console.log(message, count);
}

const messageExamples = JSON.parse(readFileSync("message_examples.json"));

server.on("connection", (socket) => {
  const messageType = Object.keys(messageExamples);
  const message =
    messageExamples[messageType[(messageType.length * Math.random()) << 0]];
  socket.send(JSON.stringify(message));
});

const socket = new WebSocket(AIS_API_URL);

socket.addEventListener("error", (event) => {
  console.error(event);
});
socket.addEventListener("message", (event) => {
  const aisMessage = JSON.parse(event.data);
  callback(aisMessage);
});
socket.addEventListener("close", () => {
 console.log("Closing with ", count, " calls" );
});

Read file is this one, could be any as long as it's an object. This prints

{
  Message: {
    AddressedBinaryMessage: {
      ApplicationID: [Object],
      BinaryData: '\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x01\x01\x00\x01\x01\x01\x01\x01\x01\x00\x01\x00\x01\x01\x01\x01\x00\x00\x01\x00\x01\x00\x01\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\x01\x00\x01\x00\x01\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x01\x01\x00\x01\x01\x00\x00\x00\x01\x01\x01\x01\x01',
      DestinationID: 2242174,
      MessageID: 6,
      RepeatIndicator: 1,
      Retransmission: true,
      Sequenceinteger: 0,
      Spare: false,
      UserID: 2242174,
      Valid: true
    }
  },
  MessageType: 'AddressedBinaryMessage',
  MetaData: {
    MMSI: 2242174,
    MMSI_String: 2242174,
    ShipName: '',
    latitude: 36.714150000000004,
    longitude: -4.414563333333334,
    time_utc: '2023-10-15 09:51:08.344744901 +0000 UTC'
  }
} 1

(That's essentially one of the stored messages, could have been any other) And exits, having been called a single time as the title of the post says. Is there anything I'm missing here? How could I get the server to run continuously?

from mock-socket.

JJ avatar JJ commented on June 3, 2024

Just left here for reference. You need to send via the socket everything you want to emit as soon as the connection is made:

server.on("connection", (socket) => {
  console.log("connected " + socket.url);
  for (const message of Object.keys(messageExamples)) {
    socket.send(JSON.stringify(messageExamples[message]));
  }
});

from mock-socket.

Atrue avatar Atrue commented on June 3, 2024

So what's the issue? You can send as many messages as you want
https://github.com/thoov/mock-socket#server-methods
Your example send the only message once the socket is connected and everything is handled properly

from mock-socket.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.