Transmission plus immédiate via websockets
This commit is contained in:
@ -3,7 +3,7 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
||||
import { LocationObject } from 'expo-location'
|
||||
|
||||
export type PlayerLocation = {
|
||||
id: number
|
||||
id?: number
|
||||
playerId: number
|
||||
longitude: number
|
||||
latitude: number
|
||||
@ -49,10 +49,14 @@ export const locationSlice = createSlice({
|
||||
},
|
||||
setLastPlayerLocations: (state, action: PayloadAction<PlayerLocation[]>) => {
|
||||
state.lastPlayerLocations = action.payload
|
||||
},
|
||||
setLastPlayerLocation: (state, action: PayloadAction<PlayerLocation>) => {
|
||||
state.lastPlayerLocations = state.lastPlayerLocations.filter(playerLoc => playerLoc.playerId !== action.payload.playerId)
|
||||
state.lastPlayerLocations.push(action.payload)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const { setLastLocation, unqueueLocation, setLastPlayerLocations } = locationSlice.actions
|
||||
export const { setLastLocation, unqueueLocation, setLastPlayerLocation, setLastPlayerLocations } = locationSlice.actions
|
||||
|
||||
export default locationSlice.reducer
|
||||
|
@ -1,9 +1,10 @@
|
||||
import * as Location from 'expo-location'
|
||||
import * as TaskManager from 'expo-task-manager'
|
||||
import { Platform } from 'react-native'
|
||||
import { setLastLocation } from './features/location/locationSlice'
|
||||
import { PlayerLocation, setLastLocation } from './features/location/locationSlice'
|
||||
import store from './store'
|
||||
import { useEffect } from 'react'
|
||||
import { socket } from './socket'
|
||||
|
||||
const LOCATION_TASK = "fetch-geolocation"
|
||||
|
||||
@ -13,7 +14,23 @@ TaskManager.defineTask(LOCATION_TASK, async ({ data, error }: any) => {
|
||||
return
|
||||
}
|
||||
const { locations } = data
|
||||
store.dispatch(setLastLocation(locations.at(-1)))
|
||||
const lastLoc: Location.LocationObject = locations.at(-1)
|
||||
store.dispatch(setLastLocation(lastLoc))
|
||||
console.log("sending-loc", lastLoc, socket.active)
|
||||
const playerId = store.getState().game.playerId
|
||||
if (socket.active && playerId) {
|
||||
const lastLocToSend: PlayerLocation = {
|
||||
playerId: playerId,
|
||||
longitude: lastLoc.coords.longitude,
|
||||
latitude: lastLoc.coords.latitude,
|
||||
speed: lastLoc.coords.speed ?? 0,
|
||||
accuracy: lastLoc.coords.accuracy ?? 0,
|
||||
altitude: lastLoc.coords.accuracy ?? 0,
|
||||
altitudeAccuracy: lastLoc.coords.altitudeAccuracy ?? 0,
|
||||
timestamp: new Date(lastLoc.timestamp).toISOString(),
|
||||
}
|
||||
socket.emit('last-location', { playerId: playerId, loc: lastLocToSend })
|
||||
}
|
||||
})
|
||||
|
||||
export async function startGeolocationService(): Promise<void | (() => void)> {
|
||||
|
5
client/utils/socket.ts
Normal file
5
client/utils/socket.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { io } from 'socket.io-client'
|
||||
|
||||
export const socket = io("http://192.168.1.198:3000", {
|
||||
reconnection: true,
|
||||
})
|
Reference in New Issue
Block a user