Advertisement
AnatolyZadvernyak

Untitled

Apr 6th, 2020
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <meta charset="utf-8" />
  3. <title>WebSocket Test</title>
  4. <script language="javascript" type="text/javascript">
  5.  
  6. // var wsUri = "ws://192.168.0.69:8080/rrr";
  7. var wsUri = "ws://176.37.84.130:8080/user1";
  8. var output;
  9. var no = 1;
  10. var toStr = "{'to':'user2', 'sdp': 'blablabla'}";
  11.  
  12. function init()
  13. {
  14. output = document.getElementById("output");
  15. testWebSocket();
  16. }
  17.  
  18. function testWebSocket()
  19. {
  20. websocket = new WebSocket(wsUri);
  21. websocket.onopen = function(evt) { onOpen(evt) };
  22. websocket.onclose = function(evt) { onClose(evt) };
  23. websocket.onmessage = function(evt) { onMessage(evt) };
  24. websocket.onerror = function(evt) { onError(evt) };
  25. }
  26.  
  27. function onOpen(evt)
  28. {
  29. writeToScreen("CONNECTED");
  30. //doSend("WebSocket rocks");
  31.  
  32. }
  33.  
  34. function onClose(evt)
  35. {
  36. writeToScreen("DISCONNECTED");
  37. }
  38.  
  39. function onMessage(evt)
  40. {
  41. writeToScreen('<span style="color: blue;">RESPONSE: ' + evt.data+'</span>');
  42. }
  43.  
  44. function onError(evt)
  45. {
  46. writeToScreen('<span style="color: red;">ERROR:</span> ' + evt.data);
  47. }
  48.  
  49. function doSend(message)
  50. {
  51. no++;
  52. writeToScreen("SENT: " + message);
  53. websocket.send(message);
  54. }
  55.  
  56. function doClose() {
  57. websocket.close();
  58. }
  59.  
  60. function writeToScreen(message)
  61. {
  62. var pre = document.createElement("p");
  63. pre.style.wordWrap = "break-word";
  64. pre.innerHTML = message;
  65. output.appendChild(pre);
  66. }
  67.  
  68. window.addEventListener("load", init, false);
  69.  
  70. </script>
  71.  
  72. <h2>WebSocket Test</h2>
  73.  
  74. <button id="btnSend" onclick="doSend(no)">Еще</button>
  75. <button onClick="doClose()">Стоп</button>
  76. <button onClick="doSend('bye')">Bye</button>
  77. <button onClick="doSend(toStr)">Name</button>
  78.  
  79.  
  80. <div id="output"></div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement