Permalink
Browse files

first version

  • Loading branch information...
0 parents commit 65e1d45590f825bcddf99d50eb936f15cffec636 @contra contra committed Jul 1, 2012
Showing 1,285 changed files with 157,984 additions and 0 deletions.
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2012 Fractal <contact@wearefractal.com>
+
+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.
105 README.md
@@ -0,0 +1,105 @@
+![status](https://secure.travis-ci.org/wearefractal/pulsar.png?branch=master)
+
+## Information
+
+<table>
+<tr>
+<td>Package</td>
+<td>pulsar</td>
+</tr>
+<tr>
+<td>Description</td>
+<td>Events/PubSub via WebSockets</td>
+</tr>
+<tr>
+<td>Node Version</td>
+<td>>= 0.4</td>
+</tr>
+</table>
+
+## Example
+
+### Server
+
+```javascript
+var Pulsar = require('pulsar');
+var server = http.createServer().listen(8080);
+var pulse = new Pulsar(server);
+
+channel = pulsar.channel('test');
+channel.on('ping', function (client, num) {
+ client.emit('pong', num);
+});
+```
+
+### Client
+
+```javascript
+var pulsar = new Pulsar();
+channel = pulsar.channel('test');
+channel.emit('ping', 2);
+channel.on('pong', function(num){
+ # num === 2
+});
+```
+
+## Server Usage
+
+### Create
+
+```
+-- Options --
+path - prefix path (default: "/pulsar")
+resource - change to allow multiple servers for one prefix (default: "default")
+```
+
+```javascript
+var Pulsar = require('pulsar');
+var pulsar = new Pulsar(8080, {options});
+```
+
+## Client Usage
+
+### Create
+
+```
+-- Options --
+host - server location (default: window.location.hostname)
+port - server port (default: window.location.port)
+secure - use SSL (default: window.location.protocol)
+path - prefix path (default: "/pulsar")
+resource - change to allow multiple servers for one endpoint (default: "default")
+```
+
+```javascript
+var pulsar = new Pulsar({options});
+```
+
+## Examples
+
+You can view a chatroom example and more in the [example folder.](https://github.com/wearefractal/pulsar/tree/master/examples)
+
+## LICENSE
+
+(MIT License)
+
+Copyright (c) 2012 Fractal <contact@wearefractal.com>
+
+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.
16 build
@@ -0,0 +1,16 @@
+# compile
+rm -rf dist
+mkdir dist
+coffee -o dist -c client/*
+
+# build
+node_modules/.bin/browserbuild -g Pulsar -m Pulsar -b dist/ node_modules/engine.io-client-f/lib/* dist/* > pulsar.js
+
+# minify
+node_modules/.bin/uglifyjs -nc --unsafe -mt -o pulsar.min.js pulsar.js
+echo "File size (minified): " && cat pulsar.min.js | wc -c
+echo "File size (gzipped): " && cat pulsar.min.js | gzip -9f | wc -c
+cp ./pulsar.js ./examples/chat/pulsar.js
+
+# clean
+rm -rf dist
@@ -0,0 +1,51 @@
+class Channel
+ constructor: (@name, @socket) ->
+ @socket.send JSON.stringify
+ channel: @name
+ action: 'join'
+ @socket.on 'message', (msg) =>
+ try
+ {channel, event, args} = JSON.parse msg
+ return unless channel is @name
+ args = [args] unless Array.isArray args
+ @realEmit event, args...
+ catch e
+ throw e
+
+ events: {}
+ realEmit: (event, args...) ->
+ return false unless @events[event]
+ l args... for l in @events[event]
+ return true
+
+ emit: (event, args...) ->
+ @socket.send JSON.stringify
+ channel: @name
+ event: event
+ args: args
+ return true
+
+ addListener: (event, listener) ->
+ @emit 'newListener', event, listener
+ (@events[event]?=[]).push listener
+ return @
+
+ on: @::addListener
+
+ once: (event, listener) ->
+ fn = =>
+ @removeListener event, fn
+ listener arguments...
+ @on event, fn
+ return @
+
+ removeListener: (event, listener) ->
+ return @ unless @events[event]
+ @events[event] = (l for l in @events[event] when l isnt listener)
+ return @
+
+ removeAllListeners: (event) ->
+ delete @events[event]
+ return @
+
+module.exports = Channel
@@ -0,0 +1,52 @@
+isBrowser = typeof window isnt 'undefined'
+eio = require (if isBrowser then 'node_modules/engine.io-client-f/lib/engine.io-client' else 'engine.io-client-f')
+Channel = require './Channel'
+
+class Pulsar extends eio.EventEmitter
+ constructor: (@options={}) ->
+ if isBrowser
+ @options.host ?= window.location.hostname
+ @options.port ?= (if window.location.port.length > 0 then parseInt window.location.port else 80)
+ @options.secure ?= (window.location.protocol is 'https:')
+
+ @options.transports ?= ["websocket", "polling"]
+ @options.path ?= '/pulsar'
+ @options.forceBust ?= true
+ @options.debug ?= false
+
+ @socket = new eio.Socket @options
+ @socket.on 'open', @handleOpen
+ @socket.on 'error', @handleError
+ @socket.on 'message', @handleMessage
+ @socket.on 'close', @handleClose
+ return
+
+ connected: false
+
+ channels: {}
+ channel: (name) => @channels[name] ?= new Channel name, @socket
+
+ disconnect: =>
+ @socket.close()
+ return
+
+ connect: =>
+ @socket.open()
+ return
+
+ # Event handlers
+ handleOpen: =>
+ @connected = true
+ @emit 'open'
+ return
+
+ handleClose: (args...) =>
+ @connected = false
+ @emit 'close', args...
+ return
+
+if typeof define is 'function'
+ define -> Pulsar
+
+window.Pulsar = Pulsar if isBrowser
+module.exports = Pulsar
@@ -0,0 +1,115 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+<title>Vein Chat</title>
+<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
+<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
+<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>
+<script src="https://d1fxtkz8shb9d2.cloudfront.net/sockjs-0.2.js"></script>
+<script src="vein.js"></script>
+
+<script>
+function appendMessage(name, message) {
+ $("#incomingMessages").append("<div class='message'><span class='username'>" + name + ":</span> " + message + "</div>");
+ $("#incomingMessages").scrollTop($("#incomingMessages")[0].scrollHeight);
+};
+
+var vein = new Vein({path:'/chat', debug:true});
+vein.ready(function (services) {
+ console.log("Connected - Available services: " + services);
+
+ var name = vein.cookie('username');
+
+ //Listeners
+ vein.subscribe.join(function (name) {
+ appendMessage('Server', name + ' has joined the room');
+ });
+ vein.subscribe.leave(function (name) {
+ appendMessage('Server', name + ' has left the room');
+ });
+ vein.subscribe.message(function (name, message) {
+ appendMessage(name, message);
+ });
+ window.onunload = function () {
+ vein.leave();
+ };
+
+ //UI garbage
+ $("#chatNameButton").click(function () {
+ $(location).attr('href', "#chatroomListPage");
+ });
+
+ $("#chatrooms li a").click(function () {
+ if (!name){
+ name = prompt("Enter your name", "");
+ }
+ vein.join(name, function (err) {
+ $(location).attr('href', "#chatPage");
+ if (err) {
+ return alert(err);
+ }
+ });
+ });
+
+ $("#chatSendButton").click(function () {
+ vein.message($("#messageText").val(), function () {
+ $("#messageText").val("");
+ });
+ });
+
+});
+
+vein.close(function () {
+ appendMessage('Server', 'Connection lost');
+});
+</script>
+
+<style>
+.message
+{
+padding: 5px 5px 5px 5px;
+}
+.username
+{
+font-weight: strong;
+color: #0f0;
+}
+.msgContainerDiv
+{
+overflow-y: scroll;
+height: 300px;
+}
+</style>
+</head>
+ <body>
+ <!-- #### CHATROOM LIST page #### -->
+ <div data-role="page" id="chatroomListPage" data-role="page" data-theme="a">
+ <div data-role="header">
+ <h1>Chattr</h1>
+ </div>
+ <div data-role="content">
+ <ul id="chatrooms" data-role="listview" data-inset="true" data-theme="a" data-dividertheme="a">
+ <li data-role="list-divider">Available Chats</li>
+ <li><a id="1022031">Main Chat</a></li>
+ </ul>
+ </div>
+ </div>
+ <!-- #### CHAT page #### -->
+ <div data-role="page" id="chatPage" data-role="page" data-theme="a">
+ <div data-role="header">
+ <h1>Main Chat</h1>
+ </div>
+ <div data-role="content">
+ <div id="incomingMessages" name="incomingMessages" class="msgContainerDiv"></div>
+ <table width="100%">
+ <tr width="100%">
+ <td width="100%"><input type="text" name="messageText" id="messageText" /></td>
+ <td>
+ <div class="ui-block-b"><button data-theme="a" id="chatSendButton" name="chatSendButton" />&raquo;</input></div>
+ </td>
+ </tr>
+ </table>
+ </div>
+ </div>
+ </body>
+</html>
Oops, something went wrong.

0 comments on commit 65e1d45

Please sign in to comment.