PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/socket.io/socket.io-tests.ts

https://github.com/vilic/DefinitelyTyped
TypeScript | 145 lines | 117 code | 28 blank | 0 comment | 1 complexity | 3ecaa8dbc836539c241ca81f9f23f041 MD5 | raw file
Possible License(s): MIT
  1. import socketIO = require('socket.io');
  2. function testUsingWithNodeHTTPServer() {
  3. var app = require('http').createServer(handler);
  4. var io = socketIO(app);
  5. var fs = require('fs');
  6. app.listen(80);
  7. function handler(req: any, res: any) {
  8. fs.readFile(__dirname + '/index.html',
  9. function (err: any, data: any) {
  10. if (err) {
  11. res.writeHead(500);
  12. return res.end('Error loading index.html');
  13. }
  14. res.writeHead(200);
  15. res.end(data);
  16. });
  17. }
  18. io.on('connection', function (socket) {
  19. socket.emit('news', { hello: 'world' });
  20. socket.on('my other event', function (data: any) {
  21. console.log(data);
  22. });
  23. });
  24. }
  25. function testUsingWithExpress() {
  26. var app = require('express')();
  27. var server = require('http').Server(app);
  28. var io = socketIO(server);
  29. server.listen(80);
  30. app.get('/', function (req: any, res: any) {
  31. res.sendfile(__dirname + '/index.html');
  32. });
  33. io.on('connection', function (socket) {
  34. socket.emit('news', { hello: 'world' });
  35. socket.on('my other event', function (data: any) {
  36. console.log(data);
  37. });
  38. });
  39. }
  40. function testUsingWithTheExpressFramework() {
  41. var app = require('express').createServer();
  42. var io = socketIO(app);
  43. app.listen(80);
  44. app.get('/', function (req: any, res: any) {
  45. res.sendfile(__dirname + '/index.html');
  46. });
  47. io.on('connection', function (socket) {
  48. socket.emit('news', { hello: 'world' });
  49. socket.on('my other event', function (data: any) {
  50. console.log(data);
  51. });
  52. });
  53. }
  54. function testSendingAndReceivingEvents() {
  55. var io = socketIO(80);
  56. io.on('connection', function (socket) {
  57. io.emit('this', { will: 'be received by everyone' });
  58. socket.on('private message', function (from: any, msg: any) {
  59. console.log('I received a private message by ', from, ' saying ', msg);
  60. });
  61. socket.on('disconnect', function () {
  62. io.sockets.emit('user disconnected');
  63. });
  64. });
  65. }
  66. function testRestrictingYourselfToANamespace() {
  67. var io = socketIO.listen(80);
  68. var chat = io
  69. .of('/chat')
  70. .on('connection', function (socket) {
  71. socket.emit('a message', {
  72. that: 'only'
  73. , '/chat': 'will get'
  74. });
  75. chat.emit('a message', {
  76. everyone: 'in'
  77. , '/chat': 'will get'
  78. });
  79. });
  80. var news = io
  81. .of('/news')
  82. .on('connection', function (socket) {
  83. socket.emit('item', { news: 'item' });
  84. });
  85. }
  86. function testSendingVolatileMessages() {
  87. var io = socketIO.listen(80);
  88. io.sockets.on('connection', function (socket) {
  89. var tweets = setInterval(function () {
  90. socket.volatile.emit('bieber tweet', {});
  91. }, 100);
  92. socket.on('disconnect', function () {
  93. clearInterval(tweets);
  94. });
  95. });
  96. }
  97. function testSendingAndGettingData() {
  98. var io = socketIO.listen(80);
  99. io.sockets.on('connection', function (socket) {
  100. socket.on('ferret', function (name: any, fn: any) {
  101. fn('woot');
  102. });
  103. });
  104. }
  105. function testBroadcastingMessages() {
  106. var io = socketIO.listen(80);
  107. io.sockets.on('connection', function (socket) {
  108. socket.broadcast.emit('user connected');
  109. });
  110. }
  111. function testUsingItJustAsACrossBrowserWebSocket() {
  112. var io = socketIO.listen(80);
  113. io.sockets.on('connection', function (socket) {
  114. socket.on('message', function () { });
  115. socket.on('disconnect', function () { });
  116. });
  117. }