gameMain.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <template>
  2. <div class="game-container">
  3. <game-notice></game-notice>
  4. <game-scene></game-scene>
  5. <game-desktop></game-desktop>
  6. </div>
  7. </template>
  8. <script>
  9. import { getUrlParam } from '@/util/util'
  10. import { getWsUrl } from '@/util/contract.js'
  11. import WsManager from '@/util/wsManager.js'
  12. import gameNotice from './notice'
  13. import gameDesktop from './desktop'
  14. import gameScene from './scene'
  15. import API from '@/api'
  16. import Bus from '@/js/bus'
  17. export default {
  18. name: 'gameMain',
  19. data () {
  20. return {
  21. }
  22. },
  23. components: {
  24. gameNotice,
  25. gameDesktop,
  26. gameScene
  27. },
  28. computed: {
  29. account () {
  30. return this.$store.state.account
  31. }
  32. },
  33. watch: {
  34. account (to, from) {
  35. let name = to.name || ''
  36. this.socketConnect(name)
  37. }
  38. },
  39. methods: {
  40. // 连接socket
  41. socketConnect (name) {
  42. if (!window.WebSocket) {
  43. console.error('Error: WebSocket is not supported .')
  44. return
  45. }
  46. let host = getWsUrl() + '?player=' + name
  47. if (this.socket) {
  48. this.socket.destroy()
  49. this.socket = null
  50. }
  51. this.socket = new WsManager(host, {
  52. autoConnect: true, // 自动连接
  53. reconnection: true, // 断开自动重连
  54. reconnectionDelay: 5000, // 重连间隔时间,单位秒
  55. keepAliveContent: JSON.stringify({ act: 'alive', player: name }) // 心跳包内容
  56. })
  57. this.socket.on('open', res => {})
  58. this.socket.on('message', (data) => {
  59. data = JSON.parse(data)
  60. if (data.channel === 'k3:new_offer') {
  61. this.$store.commit('setNewOffer', data.data)
  62. }
  63. if (data.channel === 'k3:new_open') {
  64. this.$store.commit('setNewOpen', data.data)
  65. }
  66. if (data.channel === 'global:new_auction') {
  67. Bus.$emit('update:auctioninfo')
  68. }
  69. })
  70. }
  71. },
  72. created () {
  73. let name = getUrlParam('a')
  74. if (name) {
  75. this.$store.commit('setInvitee', name.replace('/', ''))
  76. }
  77. // 获取GT汇率
  78. API.game.getGtRate().then(({ data }) => {
  79. this.$store.commit('setGtRate', data.data.dig_rate / 10000)
  80. })
  81. }
  82. }
  83. </script>
  84. <style lang="scss">
  85. .game-container{
  86. position: relative;
  87. }
  88. </style>