Permalink
Browse files

added socket.io client library

  • Loading branch information...
1 parent 935d3aa commit cfd9e9a5d822e9d563393cbbd5fb8d4e68c1ed51 Marvin Killing committed Jun 7, 2010
View
@@ -0,0 +1 @@
+client/client.js
View
@@ -2,9 +2,7 @@ sys: require "sys"
fs: require "fs"
coffee: require './lib/coffee/coffee-script'
-client_files: {"client/index.html": "out/index.html",
- "client/client.coffee": "out/client.js",
- "lib/jQuery-1.4.2.min.js": "out/jQuery-1.4.2.min.js"}
+client_files: {"client/client.coffee": "client/client.js"}
task 'clean', 'clean output folder', ->
for infile, outfile of client_files
View
@@ -1,6 +1,7 @@
<html>
<head>
<script type="text/javascript" src="client.js"></script>
+ <script type="text/javascript" src="jQuery-1.4.2.min.js"></script>
</head>
<body>
Hello
@@ -0,0 +1,3 @@
+[submodule "lib/vendor/web-socket-js"]
+ path = lib/vendor/web-socket-js
+ url = git://github.com/gimite/web-socket-js.git
View
@@ -0,0 +1,175 @@
+socket.io Client: Sockets for the rest of us
+============================================
+
+The `socket.io` client is basically a simple HTTP Socket interface implementation. It allows you to establish a realtime connection with a server (see `socket.io` server [here](http://github.com/LearnBoost/Socket.IO-node)), hiding the complexity of the different transports (WebSocket, Flash, forever iframe, XHR long polling, XHR multipart encoded, etc).
+
+## Features
+
+- Supports
+ - WebSocket
+ - Adobe Flash Socket
+ - ActiveX HTMLFile (IE)
+ - XHR with multipart encoding
+ - XHR with long-polling
+
+- ActionScript Socket is known not to work behind proxies, as it doesn't have access to the user agent proxy settings to implement the CONNECT HTTP method. If it fails, `socket.io` will try something else.
+
+- On a successful connection, it remembers the transport for next time (stores it in a cookie).
+
+- Small. Closure Compiled with all deps: 5.82kb (gzipped).
+
+- Easy to use! See [socket.io-node](http://github.com/LearnBoost/Socket.IO-node) for the server to connect to.
+
+## How to use
+
+In your head
+
+ <script src="/path/to/socket.io.js">
+ <script>
+ io.setPath('/path/to/socket.io/');
+ </script>
+
+In your code
+
+ socket = new io.Socket('localhost');
+ socket.connect();
+ socket.send('some data');
+ socket.addEvent('message', function(data){
+ alert('got some data' + data);
+ });
+
+For an example, check out the chat [source](https://github.com/LearnBoost/Socket.IO-node/blob/master/test/chat.html).
+
+## Notes
+
+IMPORTANT! When checking out the git repo, make sure to include the submodules. One way to do it is:
+
+ git clone [repo] --recursive
+
+Another, once cloned
+
+ git submodule update --init --recursive
+
+## Documentation
+
+### io.Socket
+
+ new io.Socket(host, [options]);
+
+Options:
+
+- *port*
+
+ Current port or 80
+
+ The port `socket.io` server is attached to (defaults to the document.location port)
+
+- *resource*
+
+ socket.io
+
+ The resource is what allows the `socket.io` server to identify incoming connections by `socket.io` clients. In other words, any HTTP server can implement socket.io and still serve other normal, non-realtime HTTP requests.
+
+- *transports*
+
+ ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling']
+
+ A list of the transports to attempt to utilize (in order of preference)
+
+- *transportOptions*
+
+ {
+ someTransport: {
+ someOption: true
+ },
+ ...
+ }
+
+ An object containing (optional) options to pass to each transport.
+
+Properties:
+
+- *options*
+
+ The passed in options combined with the defaults
+
+- *connected*
+
+ Whether the socket is connected or not.
+
+- *connecting*
+
+ Whether the socket is connecting or not.
+
+- *transport*
+
+ The transport instance.
+
+Methods:
+
+- *connect*
+
+ Establishes a connection
+
+- *send(message)*
+
+ A string of data to send.
+
+- *disconnect*
+
+ Closes the connection
+
+- *addEvent(event, λ)*
+
+ Adds a listener for the event *event*
+
+- *removeEvent(event, λ)*
+
+ Removes the listener λ for the event *event*
+
+Events:
+
+- *connect*
+
+ Fired when the connection is established and the handshake successful
+
+- *message(message)*
+
+ Fired when a message arrives from the server
+
+- *close*
+
+ Fired when the connection is closed. Be careful with using this event, as some transports will fire it even under temporary, expected disconnections (such as XHR-Polling).
+
+- *disconnect*
+
+ Fired when the connection is considered disconnected.
+
+## Credits
+
+Guillermo Rauch &lt;guillermo@learnboost.com&gt;
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2010 LearnBoost &lt;dev@learnboost.com&gt;
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,55 @@
+#!/usr/bin/env node
+
+/**
+ * Socket.IO client
+ *
+ * @author Guillermo Rauch <guillermo@learnboost.com>
+ * @license The MIT license.
+ * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
+ */
+
+/*
+ * This file will help you take all the Socket.IO client files and build socket.io.js.
+ * You can later use Google Closure Compiler on it.
+ */
+
+var fs = require('fs'),
+ sys = require('sys'),
+ socket = require('./lib/io'),
+ files = [
+ 'io.js',
+ 'util/util.js',
+ 'util/object.js',
+ 'util/array.js',
+ 'util/options.js',
+ 'util/events.js',
+ 'util/json.js',
+ 'util/oo.js',
+ 'transport.js',
+ 'transports/xhr.js',
+ 'transports/websocket.js',
+ 'transports/flashsocket.js',
+ 'transports/htmlfile.js',
+ 'transports/xhr-multipart.js',
+ 'transports/xhr-polling.js',
+ 'socket.js',
+ 'vendor/web-socket-js/swfobject.js',
+ 'vendor/web-socket-js/FABridge.js',
+ 'vendor/web-socket-js/web_socket.js'
+ ],
+ content = "/** Socket.IO "+ socket.io.version +" - Built with build.js */\n";
+
+sys.log('Reading files…');
+
+files.forEach(function(file){
+ var path = __dirname + '/lib/' + file;
+ sys.log (' + ' + path);
+ content += fs.readFileSync(path) + "\n";
+});
+
+sys.log('Generating…');
+
+fs.write(fs.openSync(__dirname + '/socket.io.js', 'w'), content, 0, 'utf8');
+sys.log(' + ' + __dirname + '/socket.io.js');
+
+sys.log('All done!');
View
@@ -0,0 +1,23 @@
+/**
+ * Socket.IO client
+ *
+ * @author Guillermo Rauch <guillermo@learnboost.com>
+ * @license The MIT license.
+ * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
+ */
+
+this.io = {
+ version: '0.2.3',
+
+ setPath: function(path){
+ this.path = /\/$/.test(path) ? path : path + '/';
+
+ // this is temporary until we get a fix for injecting Flash WebSocket javascript files dynamically,
+ // as io.js shouldn't be aware of specific transports.
+ if ('WebSocket' in window){
+ WebSocket.__swfLocation = path + 'lib/vendor/web-socket-js/WebSocketMain.swf';
+ }
+ }
+};
+
+if ('jQuery' in this) jQuery.io = this.io;
@@ -0,0 +1,100 @@
+/**
+ * Socket.IO client
+ *
+ * @author Guillermo Rauch <guillermo@learnboost.com>
+ * @license The MIT license.
+ * @copyright Copyright (c) 2010 LearnBoost <dev@learnboost.com>
+ */
+
+io.Socket = ioClass({
+
+ include: [io.util.Events, io.util.Options],
+
+ options: {
+ secure: false,
+ document: document,
+ port: document.location.port || 80,
+ resource: 'socket.io',
+ transports: ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling'],
+ transportOptions: {},
+ rememberTransport: true
+ },
+
+ init: function(host, options){
+ this.host = host || document.domain;
+ this.setOptions(options);
+ this.connected = false;
+ this.connecting = false;
+ this.transport = this.getTransport();
+ if (!this.transport && 'console' in window) console.error('No transport available');
+ },
+
+ getTransport: function(){
+ var transports = this.options.transports, match;
+ if (this.options.rememberTransport){
+ match = this.options.document.cookie.match('(?:^|;)\\s*socket\.io=([^;]*)');
+ if (match) transports = [decodeURIComponent(match[1])];
+ }
+ for (var i = 0; transport = transports[i]; i++){
+ if (io.Transport[transport]
+ && io.Transport[transport].check()
+ && (!this._isXDomain() || io.Transport[transport].xdomainCheck())){
+ return new io.Transport[transport](this, this.options.transportOptions[transport] || {});
+ }
+ }
+ return null;
+ },
+
+ connect: function(){
+ if (this.transport && !this.connected && !this.connecting){
+ this.connecting = true;
+ this.transport.connect();
+ }
+ return this;
+ },
+
+ send: function(data){
+ if (!this.transport || !this.transport.connected) return this._queue(data);
+ this.transport.send(JSON.stringify([data]));
+ return this;
+ },
+
+ disconnect: function(){
+ this.transport.disconnect();
+ return this;
+ },
+
+ _queue: function(message){
+ if (!('_queueStack' in this)) this._queueStack = [];
+ this._queueStack.push(message);
+ return this;
+ },
+
+ _doQueue: function(){
+ if (!('_queueStack' in this) || !this._queueStack.length) return this;
+ this.transport.send(JSON.stringify([].concat(this._queueStack)));
+ this._queueStack = [];
+ return this;
+ },
+
+ _isXDomain: function(){
+ return this.host !== document.domain;
+ },
+
+ _onConnect: function(){
+ this.connected = true;
+ this.connecting = false;
+ this._doQueue();
+ if (this.options.rememberTransport) this.options.document.cookie = 'socket.io=' + encodeURIComponent(this.transport.type);
+ this.fireEvent('connect');
+ },
+
+ _onMessage: function(data){
+ this.fireEvent('message', data);
+ },
+
+ _onDisconnect: function(){
+ this.fireEvent('disconnect');
+ }
+
+});
Oops, something went wrong.

0 comments on commit cfd9e9a

Please sign in to comment.