UNPKG

3.49 kBJavaScriptView Raw
1
2/**
3 * socket.io
4 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
5 * MIT Licensed
6 */
7
8(function (exports) {
9
10 /**
11 * IO namespace.
12 *
13 * @namespace
14 */
15
16 var io = exports;
17
18 /**
19 * Socket.IO version
20 *
21 * @api public
22 */
23
24 io.version = '0.7.11';
25
26 /**
27 * Protocol implemented.
28 *
29 * @api public
30 */
31
32 io.protocol = 1;
33
34 /**
35 * Available transports, these will be populated with the available transports
36 *
37 * @api public
38 */
39
40 io.transports = [];
41
42 /**
43 * Keep track of jsonp callbacks.
44 *
45 * @api private
46 */
47
48 io.j = [];
49
50 /**
51 * Keep track of our io.Sockets
52 *
53 * @api private
54 */
55 io.sockets = {};
56
57 // if node
58
59 /**
60 * Expose constructors if in Node
61 */
62
63 if ('object' === typeof module && 'function' === typeof require) {
64
65 /**
66 * Expose utils
67 *
68 * @api private
69 */
70
71 io.util = require('./util').util;
72
73 /**
74 * Expose JSON.
75 *
76 * @api private
77 */
78
79 io.JSON = require('./json').JSON;
80
81 /**
82 * Expose parser.
83 *
84 * @api private
85 */
86
87 io.parser = require('./parser').parser;
88
89 /**
90 * Expose EventEmitter
91 *
92 * @api private
93 */
94
95 io.EventEmitter = process.EventEmitter;
96
97 /**
98 * Expose SocketNamespace
99 *
100 * @api private
101 */
102
103 io.SocketNamespace = require('./namespace').SocketNamespace;
104
105 /**
106 * Expose Transport
107 *
108 * @api public
109 */
110
111 io.Transport = require('./transport').Transport;
112
113 /**
114 * Default enabled transports
115 *
116 * @api public
117 */
118
119 io.transports = ['websocket', 'xhr-polling'];
120
121 /**
122 * Expose all transports
123 *
124 * @api public
125 */
126
127 io.Transport.XHR = require('./transports/xhr').XHR;
128
129 io.transports.forEach(function (t) {
130 io.Transport[t] = require('./transports/' + t)[t];
131 });
132
133 /**
134 * Expose Socket
135 *
136 * @api public
137 */
138
139 io.Socket = require('./socket').Socket;
140
141 /**
142 * Location of `dist/` directory.
143 *
144 * @api private
145 */
146
147 io.dist = __dirname + '/../dist';
148
149 /**
150 * Expose our build system which can generate
151 * socket.io files on the fly with different transports
152 *
153 * @api private
154 */
155
156 io.builder = require('../bin/builder');
157
158 }
159 // end node
160
161 /**
162 * Manages connections to hosts.
163 *
164 * @param {String} uri
165 * @Param {Boolean} force creation of new socket (defaults to false)
166 * @api public
167 */
168
169 io.connect = function (host, details) {
170 var uri = io.util.parseUri(host)
171 , uuri
172 , socket;
173
174 if ('undefined' != typeof document) {
175 uri.protocol = uri.protocol || document.location.protocol.slice(0, -1);
176 uri.host = uri.host || document.domain;
177 uri.port = uri.port || document.location.port;
178 }
179
180 uuri = io.util.uniqueUri(uri);
181
182 var options = {
183 host: uri.host
184 , secure: 'https' == uri.protocol
185 , port: uri.port || ('https' == uri.protocol ? 443 : 80)
186 , query: uri.query || ''
187 };
188
189 io.util.merge(options, details);
190
191 if (options['force new connection'] || !io.sockets[uuri]) {
192 socket = new io.Socket(options);
193 }
194
195 if (!options['force new connection'] && socket) {
196 io.sockets[uuri] = socket;
197 }
198
199 socket = socket || io.sockets[uuri];
200
201 // if path is different from '' or /
202 return socket.of(uri.path.length > 1 ? uri.path : '');
203 };
204
205})('object' === typeof module ? module.exports : (window.io = {}));