39 lines
850 B
TypeScript
39 lines
850 B
TypeScript
|
import { io } from 'socket.io-client';
|
||
|
import * as fs from 'fs';
|
||
|
|
||
|
export async function load() {
|
||
|
const credFile = './credentials.json';
|
||
|
const socket = io('https://status.neshweb.net/');
|
||
|
|
||
|
let credentials: { username: string; password: string };
|
||
|
if (fs.existsSync(credFile)) {
|
||
|
let buf = fs.readFileSync(credFile);
|
||
|
credentials = JSON.parse(buf.toString());
|
||
|
} else {
|
||
|
console.error('Credentials File does not exist, Socket.io connection will not work.');
|
||
|
}
|
||
|
|
||
|
console.log(credentials);
|
||
|
|
||
|
let token = '';
|
||
|
|
||
|
socket.on('connect', () => {
|
||
|
socket.emit(
|
||
|
'login',
|
||
|
{ username: credentials.username, password: credentials.password, token: '' },
|
||
|
(res) => {
|
||
|
token = res.token;
|
||
|
console.log('Token is:', token);
|
||
|
}
|
||
|
);
|
||
|
});
|
||
|
|
||
|
while (token == '') {
|
||
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
||
|
}
|
||
|
|
||
|
return {
|
||
|
token
|
||
|
};
|
||
|
}
|