socketConnect.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var self = null;
  2. var socket = null;
  3. var docInfo = null;
  4. mui.plusReady(function(){
  5. self = plus.webview.currentWebview();
  6. docInfo = JSON.parse(plus.storage.getItem("docInfo"));
  7. connectSocket();
  8. });
  9. //判断是否有患者参与
  10. function connectSocket(){
  11. imClient.Sessions.getParticipants(self.sessionId, function(rs){
  12. var isPatient = false;
  13. $.each(rs , function(i, v) {
  14. if(v.is_patient==1){
  15. isPatient = true;//是否有患者
  16. }
  17. });
  18. createSocketConnect(isPatient);
  19. }, function(msg){
  20. console.log("e:"+JSON.stringify(msg))
  21. mui.toast("获取成员列表失败!");
  22. })
  23. }
  24. //加载socket组件 (连接socket)
  25. function createSocketConnect(isPatient){
  26. jQuery.getScript(socketUrl+"/socket.io/socket.io.js?"+Math.random()).done(function() {
  27. socket = io.connect(socketUrl);
  28. socket.emit('login', {userId: docInfo.code, password: docInfo.code,sessionId:sessionId,clientType:"doctor"});
  29. socket.on('message', function (data) {
  30. firstMsg = data;
  31. var p2pHtml = plus.webview.getWebviewById("p2p");
  32. var p2dzixunHtml = plus.webview.getWebviewById("p2dzixun");
  33. if((p2pHtml || p2dzixunHtml)){
  34. if(data.read){//已读消息
  35. if(data.read=="all"){//将消息全部置为已读
  36. if(!isPatient){//医生和医生对聊
  37. setTimeout(function(){
  38. $("#contain").find("dl.chat-right").find(".c-msg").find(".yidu").html("已读");
  39. },100)
  40. }else{//医生和居民对聊
  41. //$("#contain").find("dl.chat-right").find(".c-msg").find(".yidu").html("居民已读");
  42. // TODO "居民已读" 临时改为 "已读"
  43. setTimeout(function(){
  44. $("#contain").find("dl.chat-right").find(".c-msg").find(".yidu").html("已读");
  45. },100)
  46. }
  47. }
  48. if(data.read=="one"){
  49. if(!isPatient){//医生和医生对聊
  50. setTimeout(function(){
  51. $("#contain").find("dl.chat-right").last().find(".yidu").html("已读");
  52. },100)
  53. }else{//医生和居民对聊
  54. // $("#contain").find("dl").last().find(".yidu").html("居民已读");
  55. // TODO "居民已读" 临时改为 "已读"
  56. setTimeout(function(){
  57. $("#contain").find("dl.chat-right").last().find(".yidu").html("已读");
  58. },100)
  59. }
  60. }
  61. }else{
  62. // getNewMsgs(false);//消息接收
  63. var arr = [];
  64. arr.push(data);
  65. getNewMsgHandle( arr, false );
  66. }
  67. }else{//消息接收
  68. if(!data.read){
  69. getNewMsgs(false);
  70. }
  71. }
  72. });
  73. socket.on('error', function (data) {
  74. console.log(data);
  75. });
  76. socket.on('ack', function (data) {
  77. console.log(data);
  78. });
  79. function getLocalTime(nS) {
  80. return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
  81. }
  82. plus.storage.setItem("imSocket",socket);
  83. plus.storage.setItem("imSessionId",sessionId);
  84. })
  85. .fail(function() {
  86. plus.nativeUI.toast("医生实时对话连接失败!",{duration:"long"});
  87. });
  88. }
  89. //断开socket连接
  90. function logoutSocketConnect(){
  91. socket.emit('logout');
  92. }