ws.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. ws_parameter = {
  2. webSocket : null
  3. };
  4. function ws_connect() {
  5. $('#result').html('');
  6. var str = '<table border="1" width="100%">';
  7. str += '<tr><td colspan="2"><a href="#" id="send">send</a>&nbsp;<a href="#" id="close">close</a></td></tr>';
  8. str += '<tr><td>message:</td><td><input type="text" id="message" style="width:95%"/></td></tr>';
  9. str += '</table>';
  10. $('#content').html(str);
  11. $('#send').click(function() {
  12. ws_send($('#message').val());
  13. });
  14. var url = location.href;
  15. url = url.substring(url.indexOf('://'));
  16. url = url.substring(0, url.indexOf('x_collaboration_assemble_websocket/'));
  17. url = 'ws' + url + 'x_collaboration_assemble_websocket/ws/collaboration';
  18. url = url + '?x-token=' + getCookie("x-token");
  19. ws_parameter.webSocket = new WebSocket(url);
  20. ws_parameter.webSocket.onopen = function(evt) {
  21. ws_onOpen(evt)
  22. };
  23. ws_parameter.webSocket.onclose = function(evt) {
  24. ws_onClose(evt)
  25. };
  26. ws_parameter.webSocket.onmessage = function(evt) {
  27. ws_onMessage(evt)
  28. };
  29. ws_parameter.webSocket.onerror = function(evt) {
  30. ws_onError(evt)
  31. };
  32. }
  33. function ws_send(messagae) {
  34. ws_parameter.webSocket.send(message);
  35. }
  36. function ws_onOpen(evt) {
  37. }
  38. function ws_onClose(evt) {
  39. }
  40. function ws_onMessage(evt) {
  41. $('#result').append(evt.data);
  42. }
  43. function ws_onError(evt) {
  44. $('#result').append(evt.data);
  45. }