I have two functions. One for creating a websocket server and one for creating a websocket client based on this protocol called ocpp, using a library called ocpp-ts.
Client:
import {BootNotificationRequest, BootNotificationResponse, OcppClient, OcppError} from "ocpp-ts";
export function createClient(port: number) {
const chargingPointSimple = new OcppClient('CP1111');
chargingPointSimple.on('error', (err: Error) => {
console.log(err.message);
});
chargingPointSimple.on('close', () => {
console.log('Connection closed');
});
chargingPointSimple.on('connect', async () => {
const boot: BootNotificationRequest = {
chargePointVendor: 'eParking',
chargePointModel: 'NECU-T2',
};
try {
const bootResp: BootNotificationResponse = await chargingPointSimple.callRequest('BootNotification', boot);
if (bootResp.status === 'Accepted') {
console.log('Bootnotification accepted');
}
} catch (e) {
if (e instanceof Error || e instanceof OcppError) {
console.error(e.message);
}
}
});
chargingPointSimple.connect(`ws://localhost:${port}/`);
return chargingPointSimple;
}
if (require.main === module) {
createClient(9220);
}
Server:
import {
OcppServer, OcppClientConnection, BootNotificationRequest, BootNotificationResponse,
} from 'ocpp-ts';
export function createServer(port: number): OcppServer {
const centralSystemSimple = new OcppServer();
centralSystemSimple.listen(9220);
centralSystemSimple.on('connection', (client: OcppClientConnection) => {
console.log(`Client ${client.getCpId()} connected`);
client.on('close', (code: number, reason: Buffer) => {
console.log(`Client ${client.getCpId()} closed connection`, code, reason.toString());
});
client.on('BootNotification', (_: BootNotificationRequest, cb: (response: BootNotificationResponse) => void) => {
const response: BootNotificationResponse = {
status: 'Accepted',
currentTime: new Date().toISOString(),
interval: 60,
};
cb(response);
});
});
return centralSystemSimple;
}
if (require.main === module) {
createServer(9220);
}
If I run these servers individually on seperate terminals, behavior works as expected. I get a message that I expect.
However when I run the servers in this test using npx jest
:
/**
* @jest-environment node
*/
import {
BootNotificationRequest,
BootNotificationResponse,
OcppClient,
OcppClientConnection,
OcppError,
OcppServer
} from "ocpp-ts"
import {createServer} from "./server";
import {createClient} from "./client";
const sleep = (ms: number | undefined) => new Promise((resolve) => setTimeout(resolve, ms));
describe("simple test", () => {
let centralSystemSimple: OcppServer
// let expressServer: ApiServer
let client: OcppClient
const wsPort: number = 9220
// const apiPort = 1340
// let wsAgent: WSChain
beforeAll(async () => {
centralSystemSimple = createServer(wsPort)
})
it("should work", async () => {
createClient(wsPort)
})
afterAll(async () => {
// await sleep(9999)
})
})
I get this error (when trying to connect the client):
TypeError: server.listenerCount is not a function
at parserOnIncoming (node:_http_server:1045:26)
at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17)
at socketOnData (node:_http_server:836:22)
at Socket.emit (node:events:507:28)
at addChunk (node:internal/streams/readable:559:12)
at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
at Socket.Readable.push (node:internal/streams/readable:390:5)
at InternalSocket.onStreamRead [as onread] (node:internal/stream_base_commons:189:23)
at InternalSocket.readData (/Users/brian.yeh/WebstormProjects/occp_test/node_modules/mitm/lib/internal_socket.js:168:8)
at InternalSocket.emit (node:events:507:28)
Relevant git repo is here if you would like to experiment:
https://github.com/pyrofolium/ocpp_test
What’s going on? Why different behavior?