Skip to content

Commit

Permalink
feat(game, db): 更新数据库的版本和socket使用
Browse files Browse the repository at this point in the history
  • Loading branch information
lovezhangchuangxin committed Dec 5, 2024
1 parent 9cd4f2c commit 18ae616
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 41 deletions.
2 changes: 1 addition & 1 deletion packages/db/src/gamedb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class GameDB extends DB {

initData() {
return {
version: '0.0.1',
version: '0.0.2',
user: [],
messages: {},
userDataMap: {},
Expand Down
4 changes: 3 additions & 1 deletion packages/game/src/components/game-map/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ const getMapObject = (
if (!socket?.value) {
return
}
socket.value
.timeout(5000)
.emit('getObjectsFromChunks', chunkIds, (err: any, response: any) => {
if (err) {
toast.error('获取游戏数据失败')
toast.error('获取地图数据失败')
console.log(err)
} else if (response.code === 0) {
mapObjectData.value = response.data
callback(mapObjectData.value!)
Expand Down
29 changes: 7 additions & 22 deletions packages/game/src/views/chat/ChatPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</template>

<script setup lang="ts">
import { onMounted, onUnmounted, ref } from 'vue'
import { io, Socket } from 'socket.io-client'
import { inject, onMounted, Ref, ref } from 'vue'
import { Socket } from 'socket.io-client'
import PersonList from './PersonList.vue'
import ChatWindow from './ChatWindow.vue'
import { message as toast } from '@/utils/message'
Expand All @@ -34,27 +34,16 @@ const personList = ref<{ id: string; username: string }[]>([
const toId = ref('')
const toName = ref('')
const messages = ref<MessageInfo[]>([])
const socket = ref<Socket | null>(null)
const socket = inject<Ref<Socket>>('socket')
onMounted(() => {
socket.value = io('', {
query: {
token: localStorage.getItem('token'),
},
path: '/ws',
})
socket.value.on('connect', () => {
console.log('connect')
})
// 从本地存储中获取已聊天用户列表
try {
const chatPersonList = JSON.parse(
localStorage.getItem('chatPersonList') || '[]',
) as string[]
if (chatPersonList.length) {
socket.value
socket?.value
.timeout(5000)
.emit(
'queryUsers',
Expand All @@ -76,24 +65,20 @@ onMounted(() => {
localStorage.setItem('chatPersonList', '[]')
}
socket.value.on('receiveChat', (fromId: string, message: MessageInfo) => {
socket?.value.on('receiveChat', (fromId: string, message: MessageInfo) => {
if (fromId === userStore.id) {
return
}
messages.value.push(message)
})
})
onUnmounted(() => {
socket.value?.disconnect()
})
const handleSelectPerson = (id: string, name: string) => {
getChat(id, name)
}
const getChat = (id: string, name: string) => {
socket.value
socket?.value
?.timeout(5000)
.emit('getChat', id, (err: any, response: ResponseData<MessageInfo[]>) => {
if (err) {
Expand All @@ -114,7 +99,7 @@ const handleSendMessage = (message: string, ok: () => void) => {
return
}
socket.value
socket?.value
?.timeout(5000)
.emit(
'sendChat',
Expand Down
23 changes: 6 additions & 17 deletions packages/game/src/views/planet/PlanetPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
</template>

<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from 'vue'
import { computed, onMounted, onUnmounted, Ref, ref } from 'vue'
import { ElTabs, ElTabPane } from 'element-plus'
import { io, Socket } from 'socket.io-client'
import { Socket } from 'socket.io-client'
import {
AllStructureData,
PlanetData,
Expand All @@ -55,8 +55,9 @@ import {
import { message as toast } from '@/utils/message'
import NumberFormat from '@/components/number/NumberFormat.vue'
import ShowPanel from './ShowPanel.vue'
import { inject } from 'vue'
const socket = ref<Socket | null>(null)
const socket = inject<Ref<Socket>>('socket')
const userData = ref<UserData>()
const planetId = ref('')
const planetData = ref<PlanetData>()
Expand Down Expand Up @@ -84,30 +85,18 @@ const allResources = computed(() => {
})
onMounted(() => {
socket.value = io('', {
query: {
token: localStorage.getItem('token'),
},
path: '/ws',
})
socket.value.on('connect', () => {
console.log('connect')
})
timerId.value = setInterval(() => {
getMyData()
}, 1000) as unknown as number
})
onUnmounted(() => {
clearInterval(timerId.value!)
socket.value?.disconnect()
})
// 获取我的游戏数据
const getMyData = () => {
socket.value?.timeout(5000).emit('getMyData', (err: any, response: any) => {
socket?.value?.timeout(5000).emit('getMyData', (err: any, response: any) => {
if (err) {
toast.error('获取游戏数据失败')
} else if (response.code === 0) {
Expand Down Expand Up @@ -143,7 +132,7 @@ const addOperation = (
return
}
socket.value
socket?.value
?.timeout(5000)
.emit('addOperation', params, (err: any, response: any) => {
if (err) {
Expand Down

0 comments on commit 18ae616

Please sign in to comment.