Skip to content

Commit bbeb117

Browse files
committed
Add server.proxy to vite config to automatically proxy web socket
1 parent ee31e13 commit bbeb117

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/services/ApiController.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import {ControllerReply_StatusCode} from '../proto/ssl_gc_rcon';
77

88
export class ApiController {
9+
private readonly apiPath = '/api/control'
910
private ws ?: WebSocket
1011
private stateConsumer: ((state: RemoteControlTeamState) => any)[] = []
1112
private errorConsumer: ((message: string) => any)[] = []
@@ -27,7 +28,7 @@ export class ApiController {
2728
}
2829

2930
constructor() {
30-
this.connect(ApiController.determineWebSocketAddress())
31+
this.connect(this.determineWebSocketAddress())
3132
}
3233

3334
public Send(request: RemoteControlToController) {
@@ -47,14 +48,9 @@ export class ApiController {
4748
this.errorConsumer.push(cb)
4849
}
4950

50-
private static determineWebSocketAddress() {
51-
if (process.env.NODE_ENV === 'development') {
52-
// use the default backend port
53-
return 'ws://localhost:8083/api/control'
54-
}
55-
// UI and backend are served on the same host+port on production builds
51+
private determineWebSocketAddress() {
5652
const protocol = window.location.protocol === 'http:' ? 'ws:' : 'wss:';
57-
return protocol + '//' + window.location.hostname + ':' + window.location.port + '/api/control'
53+
return protocol + '//' + window.location.hostname + ':' + window.location.port + this.apiPath
5854
}
5955

6056
private connect(address: string) {

vite.config.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
import { defineConfig } from 'vite'
1+
import {defineConfig} from 'vite'
22
import vue from '@vitejs/plugin-vue'
33

44
// https://vitejs.dev/config/
55
export default defineConfig({
6-
plugins: [vue()]
6+
plugins: [vue()],
7+
server: {
8+
proxy: {
9+
'/api': {
10+
target: 'http://localhost:8083',
11+
changeOrigin: true,
12+
secure: false,
13+
ws: true,
14+
}
15+
}
16+
}
717
})

0 commit comments

Comments
 (0)