You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ws.js 1.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. function WSSHClient() {
  2. };
  3. WSSHClient.prototype._generateEndpoint = function () {
  4. if (window.location.protocol == 'https:') {
  5. var protocol = 'wss://';
  6. } else {
  7. var protocol = 'ws://';
  8. }
  9. var endpoint = protocol + window.location.host + '/ws';
  10. return endpoint;
  11. };
  12. WSSHClient.prototype.connect = function (options) {
  13. var endpoint = this._generateEndpoint();
  14. if (window.WebSocket) {
  15. this._connection = new WebSocket(endpoint);
  16. }
  17. else if (window.MozWebSocket) {
  18. this._connection = MozWebSocket(endpoint);
  19. }
  20. else {
  21. options.onError('WebSocket Not Supported');
  22. return;
  23. }
  24. this._connection.onopen = function () {
  25. options.onConnect();
  26. };
  27. this._connection.onmessage = function (evt) {
  28. var data = evt.data.toString()
  29. options.onData(data);
  30. };
  31. this._connection.onclose = function (evt) {
  32. options.onClose();
  33. };
  34. };
  35. WSSHClient.prototype.close = function () {
  36. this._connection.close();
  37. };
  38. WSSHClient.prototype.send = function (data) {
  39. this._connection.send(JSON.stringify(data));
  40. };
  41. WSSHClient.prototype.sendInitData = function (options) {
  42. var data = {
  43. hostname: options.host,
  44. port: options.port,
  45. username: options.username,
  46. password: options.password
  47. };
  48. this._connection.send(JSON.stringify({"tp": "init", "data": options}))
  49. console.log("发送初始化数据:" + options)
  50. }
  51. WSSHClient.prototype.sendClientData = function (data) {
  52. this._connection.send(JSON.stringify({"tp": "client", "data": data}))
  53. console.log("发送客户端数据:" + data)
  54. }
  55. var client = new WSSHClient();

No Description

Contributors (1)