AdminCommands.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div>
  3. <h3>Admin Session</h3>
  4. <div v-for="request in requestCmds" :key="request.keyword">
  5. <button @click="sendRequest(request)">{{ request.label }}</button>
  6. </div>
  7. <div v-for="response in responses" :key="response.cnt">
  8. <pre>{{ response.data }}</pre>
  9. </div>
  10. </div>
  11. </template>
  12. <script lang="ts">
  13. import { Component, Prop, Vue } from 'vue-property-decorator'
  14. import * as io from 'socket.io-client';
  15. @Component
  16. export default class AdminCommands extends Vue {
  17. @Prop() public connection;
  18. @Prop() public session;
  19. idToken: string = ''
  20. requestCmds = [
  21. { keyword: 'login', label: 'getCurrentLogins' },
  22. // { keyword: 'conn', label: 'getCurrentConnections' },
  23. // { keyword: 'sub', label: 'getSubscriptions' },
  24. { keyword: 'req', label: 'getRequests' },
  25. // { keyword: 'res', label: 'getResponses' },
  26. ];
  27. socket = null
  28. responses = []
  29. url = 'http://localhost:3011'
  30. cnt = 0
  31. setResponse(data) {
  32. this.responses = []
  33. if(Array.isArray(data) == false || data.length == 0) {
  34. this.cnt++
  35. this.responses.push({
  36. cnt: this.cnt,
  37. data: 'no results'
  38. })
  39. return
  40. }
  41. for (let index = 0; index < data.length; index++) {
  42. this.cnt++
  43. this.responses.push({
  44. cnt: this.cnt,
  45. data: JSON.stringify(
  46. this.parseData(data[index]),
  47. null, 3
  48. )
  49. })
  50. }
  51. }
  52. parseData(data) {
  53. if( data?.token ) {
  54. return this.parseLogin(data)
  55. }
  56. return this.parseRequest(data)
  57. }
  58. parseLogin(data) {
  59. return {
  60. "token": data.token,
  61. "lastActionOn": data.lastActionOn,
  62. "connections": data.connections.map(c => ({
  63. "transport": c.client.transport,
  64. "id": c.client.id,
  65. }))
  66. }
  67. }
  68. // x = [
  69. // {
  70. // "id": "9z55r61",
  71. // "token": "9z55r67016",
  72. // "lastActionOn": "2021-07-17T13:25:04.406Z",
  73. // "timeout": 10000,
  74. // "connections": [
  75. // {
  76. // "isDone": false,
  77. // "client": {
  78. // "transport": "socketio",
  79. // "id": "O6vmYhBbpHiDmibmAAAE",
  80. // "address": "::1",
  81. // "url": "/socket.io/?EIO=3&transport=polling&t=Ngqf3qf"
  82. // }
  83. // }
  84. // ]
  85. // }
  86. parseRequest(data) {
  87. const result = {
  88. ucpId: data.Message.header.security.ucpId,
  89. receivedDate: data.ReceivedDateTime,
  90. messageID: data.Message.header.messageID,
  91. messageType: data.Message.header.messageType,
  92. }
  93. if( result.messageType == 'Command' ) {
  94. result['command'] = data.Message.header.command
  95. if(result['command']=='Start') {
  96. result['ucpId'] = null
  97. }
  98. } else {
  99. result['query'] = data.Message.header.query
  100. }
  101. return result
  102. }
  103. connect(){
  104. const socket = io.connect(this.url)
  105. let cnt = 0;
  106. socket.on('admin_response', (data) => {
  107. cnt++;
  108. this.setResponse(data?.data);
  109. })
  110. socket.on('connect', (ws) => {
  111. console.info('admin connected', ws, socket?.id)
  112. });
  113. socket.connect();
  114. this.socket = socket;
  115. }
  116. sendRequest(request) {
  117. const keyword = request.keyword
  118. const options = {}
  119. this.socket.emit('admin_request',{ request: keyword, options }, (data)=>{
  120. console.log({ got: data })
  121. })
  122. }
  123. loginDisabled(){
  124. if ( this.idToken == null ) return true
  125. if ( this.connection.socketId == null ) return true
  126. if ( this.session.ucpId != null ) return true
  127. return false
  128. }
  129. mounted(){
  130. this.connect()
  131. }
  132. }
  133. </script>
  134. <style>
  135. </style>