chat.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").withAutomaticReconnect().build();
  3. //Disable the send button until connection is established.
  4. document.getElementById("sendButton").disabled = true;
  5. var msgcount = 0;
  6. connection.on("ReceiveMessage", function (topic, message) {
  7. if (msgcount > 10) {
  8. document.getElementById("messagesList").innerHTML = "";
  9. msgcount = 0;
  10. }
  11. var li = document.createElement("li");
  12. document.getElementById("messagesList").appendChild(li);
  13. // We can assign user-supplied strings to an element's textContent because it
  14. // is not interpreted as markup. If you're assigning in any other way, you
  15. // should be aware of possible script injection concerns.
  16. li.textContent = `${topic} --> ${message}`;
  17. msgcount = msgcount + 1;
  18. });
  19. connection.start().then(function () {
  20. document.getElementById("sendButton").disabled = false;
  21. }).catch(function (err) {
  22. return console.error(err.toString());
  23. });
  24. document.getElementById("sendButton").addEventListener("click", function (event) {
  25. var topic = document.getElementById("topicInput").Data;
  26. var message = document.getElementById("messageInput").Data;
  27. connection.invoke("SendMessage", topic, message).catch(function (err) {
  28. return console.error(err.toString());
  29. });
  30. event.preventDefault();
  31. });
  32. //以下是接口交互相关的内容
  33. //小车的定时状态,多个小车,只有在线小车才有实时数据
  34. connection.on("JgrStatus", function (jgrStatus) {
  35. if (msgcount > 10) {
  36. document.getElementById("messagesList").innerHTML = "";
  37. msgcount = 0;
  38. }
  39. var li = document.createElement("li");
  40. document.getElementById("messagesList").appendChild(li);
  41. // We can assign user-supplied strings to an element's textContent because it
  42. // is not interpreted as markup. If you're assigning in any other way, you
  43. // should be aware of possible script injection concerns.
  44. li.textContent = `jgrStatus:${jgrStatus}`;
  45. msgcount = msgcount + 1;
  46. });
  47. //工位的定时状态,多个工位
  48. connection.on("StationStatus", function (stationStatus) {
  49. if (msgcount > 10) {
  50. document.getElementById("messagesList").innerHTML = "";
  51. msgcount = 0;
  52. }
  53. var li = document.createElement("li");
  54. document.getElementById("messagesList").appendChild(li);
  55. // We can assign user-supplied strings to an element's textContent because it
  56. // is not interpreted as markup. If you're assigning in any other way, you
  57. // should be aware of possible script injection concerns.
  58. li.textContent = `stationStatus:${stationStatus}`;
  59. msgcount = msgcount + 1;
  60. });
  61. //充电桩的定时状态,多个充电桩,只有在线才会回传数据
  62. connection.on("ChargingStatus", function (chargingStatus) {
  63. if (msgcount > 10) {
  64. document.getElementById("messagesList").innerHTML = "";
  65. msgcount = 0;
  66. }
  67. var li = document.createElement("li");
  68. document.getElementById("messagesList").appendChild(li);
  69. // We can assign user-supplied strings to an element's textContent because it
  70. // is not interpreted as markup. If you're assigning in any other way, you
  71. // should be aware of possible script injection concerns.
  72. li.textContent = ` chargingStatus:${chargingStatus}`;
  73. msgcount = msgcount + 1;
  74. });