This commit is contained in:
@@ -17,6 +17,7 @@ if (process.env.NODE_ENV) {
|
|||||||
const app = express();
|
const app = express();
|
||||||
const httpServer = createServer(app);
|
const httpServer = createServer(app);
|
||||||
const io = new Server(httpServer, {
|
const io = new Server(httpServer, {
|
||||||
|
path: '/api/socket.io',
|
||||||
cors: {
|
cors: {
|
||||||
origin: true,
|
origin: true,
|
||||||
credentials: true,
|
credentials: true,
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
// composables/useApi.js
|
// composables/useApi.js
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
|
|
||||||
|
function normalizeUrl(url: string): string {
|
||||||
|
return url.replace(/\/+$/, '')
|
||||||
|
}
|
||||||
|
|
||||||
export default function useApi() {
|
export default function useApi() {
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
const baseUrl = config.public.apiBaseUrl
|
const baseUrl = `${normalizeUrl(config.public.apiBaseUrl)}/api`
|
||||||
|
|
||||||
// Generic GET request
|
// Generic GET request
|
||||||
const get = async (endpoint) => {
|
const get = async (endpoint) => {
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
import { ref, onUnmounted } from 'vue'
|
import { ref, onUnmounted } from 'vue'
|
||||||
import { io } from 'socket.io-client'
|
import { io } from 'socket.io-client'
|
||||||
|
|
||||||
|
function normalizeUrl(url: string): string {
|
||||||
|
return url.replace(/\/+$/, '')
|
||||||
|
}
|
||||||
|
|
||||||
export default function useSocket() {
|
export default function useSocket() {
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
const socketUrl = `${config.public.apiBaseUrl.replace('/api', '')}`
|
const apiBaseUrl = normalizeUrl(config.public.apiBaseUrl)
|
||||||
|
|
||||||
const socket = ref(null)
|
const socket = ref(null)
|
||||||
const isConnected = ref(false)
|
const isConnected = ref(false)
|
||||||
@@ -11,7 +15,8 @@ export default function useSocket() {
|
|||||||
function initSocket() {
|
function initSocket() {
|
||||||
if (socket.value) return
|
if (socket.value) return
|
||||||
|
|
||||||
const s = io(socketUrl, {
|
const s = io(apiBaseUrl, {
|
||||||
|
path: '/api/socket.io',
|
||||||
transports: ['websocket', 'polling'],
|
transports: ['websocket', 'polling'],
|
||||||
reconnection: true,
|
reconnection: true,
|
||||||
reconnectionAttempts: 10,
|
reconnectionAttempts: 10,
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ export default defineNuxtConfig({
|
|||||||
|
|
||||||
runtimeConfig: {
|
runtimeConfig: {
|
||||||
public: {
|
public: {
|
||||||
apiBaseUrl: process.env.API_BASE_URL || 'http://localhost:5000/api'
|
apiBaseUrl: process.env.API_BASE_URL || 'http://localhost:5000'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user