Aucune description

socket.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. var socketVoice = []; //websocket对象数组
  2. var getTimeStringVoice = new Date().getTime().toString();
  3. var objNew ={}
  4. //创建socket连接功能函数
  5. function connectVoice(wsobj) {
  6. //浏览器支持?
  7. if("WebSocket" in window) {
  8. var extno = $.cookie("extno")
  9. if(!extno){
  10. return
  11. }
  12. // var extno = "1001"
  13. var host = huayi.config.translation_socket+"/"+extno
  14. console.log(host)
  15. socketVoice = new WebSocket(host);
  16. try {
  17. //连接事件
  18. socketVoice.onopen = function(msg) {
  19. console.log(wsobj + ":连接已建立!")
  20. $("#clickElementVoice").show()
  21. // videoReqExten()
  22. };
  23. //错误事件
  24. socketVoice.onerror = function(msg) {
  25. console.log("错误:" + msg.data)
  26. }
  27. //消息事件
  28. socketVoice.onmessage = function(msg) {
  29. //最后转回json对象
  30. data = eval('(' + msg.data + ')');
  31. console.log(msg.data)
  32. console.log(data.event,data.data.label,data.data.content);
  33. switch (data.event.toLowerCase()) {
  34. case "translation":
  35. translationBack(data);
  36. break; //签入
  37. }
  38. // console.log(wsobj+"消息接收:"+data.data)
  39. };
  40. //关闭事件
  41. socketVoice.onclose = function(msg) {
  42. connectVoice(1)
  43. console.log(wsobj + ":socket closed!")
  44. };
  45. } catch(ex) {
  46. log(ex);
  47. }
  48. } else {
  49. // 浏览器不支持 WebSocket
  50. alert("您的浏览器不支持 WebSocket!");
  51. }
  52. }
  53. //申请注册的分机
  54. function videoReqExten() {
  55. // objNew.Type = "VideoReqExten";
  56. // objNew.AgentID = getTimeStringVoice.substring(getTimeStringVoice.length-9);
  57. // objNew.AgentExten = "111";
  58. SendVoice()
  59. }
  60. //发送
  61. function SendVoice() {
  62. if (socketVoice.readyState != socketVoice.OPEN) {
  63. reconnect();
  64. }
  65. if (socketVoice.readyState ==socketVoice.OPEN) {
  66. console.log(new Date() + " send " + JSON.stringify(objNew));
  67. socketVoice.send(JSON.stringify(objNew));
  68. objNew.Type = "";
  69. }
  70. }
  71. function translationBack(data) {
  72. if(data.data.label === "坐席"){
  73. $('<div class="serviceChat"><img src="img/kefuTx.png"><div class="serviceChatText">'+
  74. data.data.content+'</div></div>').appendTo($(".voiceToTextContext"))
  75. } else if (data.data.label === "客户") {
  76. $('<div class="clientChat"><img src="img/youkeTx.png"><div class="clientChatText">'+
  77. data.data.content+'</div></div>').appendTo($(".voiceToTextContext"));
  78. }
  79. scrollTopFn("scrollVoice")
  80. }