Ein Projekt das es ermöglicht Beerpong über das Internet von zwei unabhängigen positionen aus zu spielen. Entstehung im Rahmen einer Praktikumsaufgabe im Fach Interaktion.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sender.hixie.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*!
  2. * ws: a node.js websocket client
  3. * Copyright(c) 2011 Einar Otto Stangvik <einaros@gmail.com>
  4. * MIT Licensed
  5. */
  6. var events = require('events')
  7. , util = require('util')
  8. , EventEmitter = events.EventEmitter;
  9. /**
  10. * Hixie Sender implementation
  11. */
  12. function Sender(socket) {
  13. events.EventEmitter.call(this);
  14. this.socket = socket;
  15. this.continuationFrame = false;
  16. this.isClosed = false;
  17. }
  18. module.exports = Sender;
  19. /**
  20. * Inherits from EventEmitter.
  21. */
  22. util.inherits(Sender, events.EventEmitter);
  23. /**
  24. * Frames and writes data.
  25. *
  26. * @api public
  27. */
  28. Sender.prototype.send = function(data, options, cb) {
  29. if (this.isClosed) return;
  30. var isString = typeof data == 'string'
  31. , length = isString ? Buffer.byteLength(data) : data.length
  32. , lengthbytes = (length > 127) ? 2 : 1 // assume less than 2**14 bytes
  33. , writeStartMarker = this.continuationFrame == false
  34. , writeEndMarker = !options || !(typeof options.fin != 'undefined' && !options.fin)
  35. , buffer = new Buffer((writeStartMarker ? ((options && options.binary) ? (1 + lengthbytes) : 1) : 0) + length + ((writeEndMarker && !(options && options.binary)) ? 1 : 0))
  36. , offset = writeStartMarker ? 1 : 0;
  37. if (writeStartMarker) {
  38. if (options && options.binary) {
  39. buffer.write('\x80', 'binary');
  40. // assume length less than 2**14 bytes
  41. if (lengthbytes > 1)
  42. buffer.write(String.fromCharCode(128+length/128), offset++, 'binary');
  43. buffer.write(String.fromCharCode(length&0x7f), offset++, 'binary');
  44. } else
  45. buffer.write('\x00', 'binary');
  46. }
  47. if (isString) buffer.write(data, offset, 'utf8');
  48. else data.copy(buffer, offset, 0);
  49. if (writeEndMarker) {
  50. if (options && options.binary) {
  51. // sending binary, not writing end marker
  52. } else
  53. buffer.write('\xff', offset + length, 'binary');
  54. this.continuationFrame = false;
  55. }
  56. else this.continuationFrame = true;
  57. try {
  58. this.socket.write(buffer, 'binary', cb);
  59. } catch (e) {
  60. this.error(e.toString());
  61. }
  62. };
  63. /**
  64. * Sends a close instruction to the remote party.
  65. *
  66. * @api public
  67. */
  68. Sender.prototype.close = function(code, data, mask, cb) {
  69. if (this.isClosed) return;
  70. this.isClosed = true;
  71. try {
  72. if (this.continuationFrame) this.socket.write(new Buffer([0xff], 'binary'));
  73. this.socket.write(new Buffer([0xff, 0x00]), 'binary', cb);
  74. } catch (e) {
  75. this.error(e.toString());
  76. }
  77. };
  78. /**
  79. * Sends a ping message to the remote party. Not available for hixie.
  80. *
  81. * @api public
  82. */
  83. Sender.prototype.ping = function(data, options) {};
  84. /**
  85. * Sends a pong message to the remote party. Not available for hixie.
  86. *
  87. * @api public
  88. */
  89. Sender.prototype.pong = function(data, options) {};
  90. /**
  91. * Handles an error
  92. *
  93. * @api private
  94. */
  95. Sender.prototype.error = function (reason) {
  96. this.emit('error', reason);
  97. return this;
  98. };