visualization_websocket.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. var socket;
  2. function openSocket(loginName) {
  3. if(typeof(WebSocket) == "undefined") {
  4. console.log("您的浏览器不支持WebSocket");
  5. }else{
  6. console.log("您的浏览器支持WebSocket");
  7. var socketUrl="http://localhost:9090/business/app/websocket/"+loginName;
  8. // var socketUrl="http://127.0.0.1/business/app/websocket/"+loginName;
  9. socketUrl=socketUrl.replace("https","ws").replace("http","ws");
  10. console.log(socketUrl);
  11. if(socket!=null){
  12. socket.close();
  13. socket=null;
  14. }
  15. socket = new WebSocket(socketUrl);
  16. //打开事件
  17. socket.onopen = function() {
  18. console.log("websocket已打开");
  19. //socket.send("这是来自客户端的消息" + location.href + new Date());
  20. };
  21. //获得消息事件
  22. socket.onmessage = function(msg) {
  23. // var text ='{"content":"String content","id":"0d44d0027e424e898f60a456ca8b8a6a","latitude":"5318807.94","longitude":"13874773.35","title":"String title"}';
  24. var message = msg.data;
  25. if(message != "" && message != null){
  26. // var json = $.parseJSON(msg.data);
  27. var json = message;
  28. console.log(message.content);
  29. console.log(json);
  30. getVisualization();
  31. }
  32. };
  33. //关闭事件
  34. socket.onclose = function() {
  35. console.log("websocket已关闭");
  36. };
  37. //发生了错误事件
  38. socket.onerror = function() {
  39. console.log("websocket发生了错误");
  40. }
  41. }
  42. }
  43. openSocket("sun11")