mirror of
https://github.com/Spythere/stacjownik.git
synced 2026-05-03 13:28:11 +00:00
przejście z WS na komunikację http
This commit is contained in:
+1
-1
@@ -65,7 +65,7 @@ export default defineComponent({
|
||||
|
||||
created() {
|
||||
this.loadLang();
|
||||
this.store.connectToAPI();
|
||||
this.store.setupAPI();
|
||||
|
||||
this.store.isOffline = !window.navigator.onLine;
|
||||
|
||||
|
||||
+21
-31
@@ -1,6 +1,5 @@
|
||||
import axios from 'axios';
|
||||
import { defineStore } from 'pinia';
|
||||
import { io } from 'socket.io-client';
|
||||
import StationRoutes from '../scripts/interfaces/StationRoutes';
|
||||
import Train from '../scripts/interfaces/Train';
|
||||
import { URLs } from '../scripts/utils/apiURLs';
|
||||
@@ -8,10 +7,11 @@ import { parseSpawns, getScheduledTrains, getStationTrains } from './utils';
|
||||
|
||||
import { OnlineScenery, ScheduledTrain, StationJSONData, StoreState } from './typings';
|
||||
|
||||
import packageInfo from '../../package.json';
|
||||
import { Websocket, API } from '../typings/api';
|
||||
import { API } from '../typings/api';
|
||||
import { Status } from '../typings/common';
|
||||
|
||||
const API_INTERVAL_MS = 30000;
|
||||
|
||||
export const useStore = defineStore('store', {
|
||||
state: () =>
|
||||
({
|
||||
@@ -32,7 +32,6 @@ export const useStore = defineStore('store', {
|
||||
trainCount: 0,
|
||||
stationCount: 0,
|
||||
|
||||
webSocket: undefined,
|
||||
isOffline: false,
|
||||
|
||||
dispatcherStatsName: '',
|
||||
@@ -245,7 +244,7 @@ export const useStore = defineStore('store', {
|
||||
});
|
||||
},
|
||||
|
||||
async connectToWebsocket() {
|
||||
async fetchActiveData() {
|
||||
if (import.meta.env.VITE_APP_WS_DEV === '1') {
|
||||
const mockWebsocketData = await import('../data/mockWebsocketData.json');
|
||||
this.dataStatuses.connection = Status.Data.Loaded;
|
||||
@@ -257,36 +256,27 @@ export const useStore = defineStore('store', {
|
||||
return;
|
||||
}
|
||||
|
||||
const socket = io(URLs.stacjownikAPI, {
|
||||
transports: ['websocket', 'polling'],
|
||||
rememberUpgrade: true,
|
||||
reconnection: true
|
||||
});
|
||||
try {
|
||||
const data = (
|
||||
await axios.get<API.ActiveData.Response>(`${URLs.stacjownikAPI}/api/getActiveData`)
|
||||
).data;
|
||||
|
||||
socket.emit('CONNECTION', { version: packageInfo.version });
|
||||
|
||||
socket.on('connect_error', () => {
|
||||
this.activeData = data;
|
||||
this.dataStatuses.connection = Status.Data.Loaded;
|
||||
this.setStatuses();
|
||||
} catch (error) {
|
||||
this.dataStatuses.connection = Status.Data.Error;
|
||||
});
|
||||
|
||||
socket.on('UPDATE', (data: Websocket.ActiveData) => {
|
||||
this.activeData = data;
|
||||
this.dataStatuses.connection = Status.Data.Loaded;
|
||||
|
||||
this.setStatuses();
|
||||
});
|
||||
|
||||
socket.emit('FETCH_DATA', { version: packageInfo.version }, (data: Websocket.ActiveData) => {
|
||||
this.dataStatuses.connection = Status.Data.Loaded;
|
||||
this.activeData = data;
|
||||
this.setStatuses();
|
||||
});
|
||||
|
||||
this.webSocket = socket;
|
||||
console.error('Wystąpił błąd podczas pobierania danych online z API!');
|
||||
}
|
||||
},
|
||||
|
||||
async connectToAPI() {
|
||||
this.connectToWebsocket();
|
||||
async setupAPI() {
|
||||
this.fetchActiveData();
|
||||
|
||||
setInterval(() => {
|
||||
this.fetchActiveData();
|
||||
}, API_INTERVAL_MS);
|
||||
|
||||
this.fetchStockInfoData();
|
||||
this.fetchDonatorsData();
|
||||
this.fetchStationsGeneralInfo();
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { Socket } from 'socket.io-client';
|
||||
import Station from '../scripts/interfaces/Station';
|
||||
import { API, Websocket } from '../typings/api';
|
||||
import { API } from '../typings/api';
|
||||
import { Status } from '../typings/common';
|
||||
|
||||
export type Availability = 'default' | 'unavailable' | 'nonPublic' | 'abandoned' | 'nonDefault';
|
||||
@@ -13,7 +12,7 @@ export interface RegionCounters {
|
||||
|
||||
export interface StoreState {
|
||||
stationList: Station[];
|
||||
activeData: Websocket.ActiveData;
|
||||
activeData: API.ActiveData.Response;
|
||||
rollingStockData?: API.RollingStock.Response;
|
||||
donatorsData: API.Donators.Response;
|
||||
|
||||
@@ -31,7 +30,6 @@ export interface StoreState {
|
||||
trainCount: number;
|
||||
stationCount: number;
|
||||
|
||||
webSocket?: Socket;
|
||||
isOffline: boolean;
|
||||
|
||||
dispatcherStatsName: string;
|
||||
|
||||
+7
-8
@@ -1,6 +1,13 @@
|
||||
import { Status } from './common';
|
||||
|
||||
export namespace API {
|
||||
export namespace ActiveData {
|
||||
export interface Response {
|
||||
activeSceneries?: API.ActiveSceneries.Response;
|
||||
trains?: API.ActiveTrains.Response;
|
||||
connectedSocketCount: number;
|
||||
}
|
||||
}
|
||||
export namespace DispatcherHistory {
|
||||
export type Response = Data[];
|
||||
|
||||
@@ -280,14 +287,6 @@ export namespace API {
|
||||
}
|
||||
}
|
||||
|
||||
export namespace Websocket {
|
||||
export interface ActiveData {
|
||||
activeSceneries?: API.ActiveSceneries.Response;
|
||||
trains?: API.ActiveTrains.Response;
|
||||
connectedSocketCount: number;
|
||||
}
|
||||
}
|
||||
|
||||
export namespace GithubAPI {
|
||||
export namespace Release {
|
||||
export interface Author {
|
||||
|
||||
Reference in New Issue
Block a user