Permalink
Browse files

simple express 3 example

  • Loading branch information...
1 parent 2fc985a commit 2181155a1d64012c6c815060bc363cfc1f68e26f @stereosteve stereosteve committed May 9, 2013
View
@@ -0,0 +1,14 @@
+Minimal example of ws-rpc
+
+setup:
+
+ npm install
+ node app.js
+
+browser example (open console to see output):
+
+ http://localhost:3000
+
+node example:
+
+ node client-example.js
View
@@ -0,0 +1,94 @@
+
+/**
+ * Module dependencies.
+ */
+
+var express = require('express')
+ , http = require('http')
+ , path = require('path');
+
+var app = express();
+var server = http.createServer(app)
+var ws = require('ws-rpc').extend(require('ws'));
+var wss = new ws.Server({ server: server });
+
+// all environments
+app.set('port', process.env.PORT || 3000);
+app.use(express.favicon());
+app.use(express.logger('dev'));
+
+app.use(wss.middleware(express));
+app.use(express.static(path.join(__dirname, 'public')));
+
+// development only
+if ('development' === app.get('env')) {
+ app.use(express.errorHandler());
+}
+
+
+
+
+
+wss.on('connect', function(client) {
+ client.message('message 1', 'arg1', 'arg2', function(err, arg1, arg2) {
+ console.log('reply to message 1', arguments);
+
+ client.message('message 2', 'arg1', 'arg2', function(err, arg1, arg2) {
+ console.log('reply to message 2', arguments);
+ if(!err) throw new Error('fail');
+
+ client.message('message 3', 'arg1', 'arg2');
+ });
+ });
+});
+
+wss.on('disconnect', function(client) {
+ console.log('client disconnected');
+});
+
+wss.on('client message 1', function(client, arg1, arg2, cb) {
+ console.log('client message 1', arg1, arg2);
+ cb(null, 'arg1', 'arg2');
+});
+
+wss.on('client message 2', function(client, arg1, arg2, cb) {
+ console.log('client message 2', arg1, arg2);
+ cb(new Error('test error'));
+});
+
+var roomA = wss.room('A');
+var roomB = wss.room('B');
+
+wss.on('client message 3', function(client, arg1, arg2) {
+ console.log('client message 3', arg1, arg2);
+
+ roomA.add(client);
+ roomB.add(client);
+
+ roomA.message('message room A', 'arg1', 'arg2');
+ roomB.message('message room B', 'arg1', 'arg2');
+
+ roomA.remove(client);
+ roomB.remove(client);
+
+ roomA.message('message room A', 'arg1', 'arg2');
+ roomB.message('message room B', 'arg1', 'arg2');
+
+ wss.message('server message', 'arg1', 'arg2');
+
+ client.message('message 4');
+});
+
+wss.on('error', function(e, client) {
+ console.log(client ? 'client' : 'server', e.stack);
+});
+
+
+
+
+
+
+
+server.listen(app.get('port'), function(){
+ console.log('Express server listening on port ' + app.get('port'));
+});
@@ -0,0 +1,62 @@
+
+var ws = require('ws-rpc').extend(require('ws'));
+var wsc = new ws.RPCWebSocket('ws://127.0.0.1:3000/');
+
+var log = console.log;
+
+wsc.on('open', function() {
+ log('connected');
+});
+
+wsc.on('close', function() {
+ log('disconnected');
+});
+
+wsc.on('error', function(e) {
+ console.log('error:', e.stack || e);
+});
+
+
+
+wsc.on('message 1', function(arg1, arg2, cb) {
+ log('message 1: ' + arg1 + ', ' + arg2);
+ cb(null, 'message1 response from client', 'arg2');
+});
+
+wsc.on('message 2', function(arg1, arg2, cb) {
+ log('message 2: ' + arg1 + ', ' + arg2);
+ cb(new Error('message2 ERROR response from client'));
+});
+
+wsc.on('message 3', function(arg1, arg2) {
+ log('message 3: ' + arg1 + ', ' + arg2);
+
+ wsc.message('client message 1', 'arg1', 'arg2', function(err, arg1, arg2) {
+ log('response to client message 1: ' + err + ', ' + arg1 + ', ' + arg2);
+ if(err) throw err;
+
+ wsc.message('client message 2', 'arg1', 'arg2', function(err, arg1, arg2) {
+ log('response to client message 2: ' + JSON.stringify(err) + ', ' + arg1 + ', ' + arg2);
+
+ wsc.message('client message 3', 'arg1', 'arg2');
+ });
+ });
+});
+
+wsc.on('message room A', function(arg1, arg2) {
+ log('message room A: ' + arg1 + ', ' + arg2);
+});
+
+wsc.on('message room B', function(arg1, arg2) {
+ log('message room B: ' + arg1 + ', ' + arg2);
+});
+
+wsc.on('server message', function(arg1, arg2) {
+ log('server message: ' + arg1 + ', ' + arg2);
+});
+
+wsc.on('message 4', function(arg1, arg2) {
+ log('message 4: ' + arg1 + ', ' + arg2);
+ //wsc.disconnect(true);
+});
+
@@ -0,0 +1,13 @@
+{
+ "name": "application-name",
+ "version": "0.0.1",
+ "private": true,
+ "scripts": {
+ "start": "node app.js"
+ },
+ "dependencies": {
+ "express": "3.2.4",
+ "ws-rpc": "0.0.6",
+ "ws": "~0.4.25"
+ }
+}
@@ -0,0 +1,67 @@
+var url = 'ws://' + window.location.host + '/'
+
+var WebSocketRPC = window.InitWebSocketRPC(WebSocket);
+var wsc = new WebSocketRPC('ws://' + window.location.host + '/');
+
+
+
+wsc.on('open', function() {
+ log('connected');
+});
+
+wsc.on('close', function() {
+ log('disconnected');
+});
+
+wsc.on('error', function(e) {
+ log(e.stack || e.message || e);
+ if(window.console)
+ console.log(e.stack || e);
+});
+
+wsc.on('message 1', function(arg1, arg2, cb) {
+ log('message 1: ' + arg1 + ', ' + arg2);
+ cb(null, 'message1 response from client', 'arg2');
+});
+
+wsc.on('message 2', function(arg1, arg2, cb) {
+ log('message 2: ' + arg1 + ', ' + arg2);
+ cb(new Error('message2 ERROR response from client'));
+});
+
+wsc.on('message 3', function(arg1, arg2) {
+ log('message 3: ' + arg1 + ', ' + arg2);
+
+ wsc.message('client message 1', 'arg1', 'arg2', function(err, arg1, arg2) {
+ log('response to client message 1: ' + err + ', ' + arg1 + ', ' + arg2);
+ if(err) throw err;
+
+ wsc.message('client message 2', 'arg1', 'arg2', function(err, arg1, arg2) {
+ log('response to client message 2: ' + JSON.stringify(err) + ', ' + arg1 + ', ' + arg2);
+
+ wsc.message('client message 3', 'arg1', 'arg2');
+ });
+ });
+});
+
+wsc.on('message room A', function(arg1, arg2) {
+ log('message room A: ' + arg1 + ', ' + arg2);
+});
+
+wsc.on('message room B', function(arg1, arg2) {
+ log('message room B: ' + arg1 + ', ' + arg2);
+});
+
+wsc.on('server message', function(arg1, arg2) {
+ log('server message: ' + arg1 + ', ' + arg2);
+});
+
+wsc.on('message 4', function(arg1, arg2) {
+ log('message 4: ' + arg1 + ', ' + arg2);
+ //wsc.disconnect(true);
+});
+
+
+function log(msg) {
+ console.log(msg)
+}
@@ -0,0 +1,12 @@
+<!doctype html>
+<html>
+ <head>
+ <title>rpc example</title>
+ </head>
+ <body>
+
+ <script src="/ws-rpc-client.js"></script>
+ <script src="/browser-example.js"></script>
+
+ </body>
+</html>

0 comments on commit 2181155

Please sign in to comment.