Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

merged chat

  • Loading branch information...
commit faeb21d44f630621bc54ef9b9ae9bd8dd3b115f4 2 parents 42a1c6f + bc575f9
Kensaku KOMATSU authored
5 app.js
@@ -20,11 +20,11 @@ app.configure(function(){
20 20 });
21 21
22 22 app.configure('development', function(){
23   - app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  23 + app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
24 24 });
25 25
26 26 app.configure('production', function(){
27   - app.use(express.errorHandler());
  27 + app.use(express.errorHandler());
28 28 });
29 29
30 30 // Routes
@@ -32,6 +32,7 @@ app.configure('production', function(){
32 32 app.get('/', routes.index);
33 33 app.get('/shorten', routes.shorten);
34 34 app.get('/imageshare', routes.imageshare);
  35 +app.get('/messageport', routes.messageport);
35 36
36 37 app.listen(process.env.PORT || process.env.VMC_APP_PORT || 3000);
37 38 console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
25 public/c.html
... ... @@ -0,0 +1,25 @@
  1 +<!doctype html>
  2 +<html>
  3 +<head>
  4 +<title>child</title>
  5 +<script src="javascripts/pipe.js"></script>
  6 +</head>
  7 +<body>
  8 +Hello, child!!
  9 +<output></output>
  10 +
  11 +<script>
  12 + var server = new Pipe.Server();
  13 + server.setup();
  14 +
  15 + setInterval(function(){
  16 + server.send(new Date().getTime());
  17 + }, 1000);
  18 +
  19 + server.onmessage = function(e) {
  20 + document.querySelector("output").innerText = e.data;
  21 + this.send("recv: "+e.data);
  22 + }
  23 +</script>
  24 +</body>
  25 +</html>
99 public/javascripts/pipe.js
... ... @@ -0,0 +1,99 @@
  1 +var Pipe = {};
  2 +
  3 +(function(global){
  4 + Pipe.Server = function(){
  5 + var setup_, send_, onmessage_, onconnect_, port_, flag = true;
  6 +
  7 + send_ = function(data) {
  8 + if(!!port_ === false) {
  9 + throw("connection not established");
  10 + }
  11 +
  12 + port_.postMessage(data);
  13 + };
  14 +
  15 + onmessage_ = function(data){
  16 + console.log("Pipe.Server.onmessage");
  17 + console.log(data);
  18 + };
  19 +
  20 + onconnect_ = function(data){
  21 + console.log("Pipe.Server.onconnect");
  22 + console.log("connection established");
  23 + };
  24 +
  25 +
  26 + setup_ = function(){
  27 + var self = this;
  28 + window.addEventListener("message", function(e) {
  29 + if(flag) {
  30 + self.onconnect(e);
  31 +
  32 + port_ = e.ports[0];
  33 + flag = false;
  34 +
  35 + port_.onmessage = function(e){
  36 + self.onmessage(e);
  37 + }
  38 + }
  39 + }, false);
  40 + }
  41 +
  42 + return {
  43 + "setup": setup_,
  44 + "onconnect" : onconnect_,
  45 + "send" : send_,
  46 + "onmessage" : onmessage_
  47 + }
  48 + }
  49 +
  50 + Pipe.Client = function(){
  51 + var setup_, send_, onmessage_, port_, ifr_, flag = true;
  52 +
  53 +
  54 + setup_ = function(url, id){
  55 + // [TODO] validates url string
  56 + if(typeof(url) !== "string") {
  57 + throw("parameter url error");
  58 + }
  59 +
  60 + ifr_ = document.createElement('iframe');
  61 + ifr_.src = url;
  62 + document.body.appendChild(ifr_);
  63 +
  64 + var self = this;
  65 +
  66 + ifr_.onload = function(e){
  67 + var channel = new MessageChannel();
  68 + port_ = channel.port1;
  69 + console.log("onload iframe");
  70 +
  71 + // [TODO] passes origin
  72 + ifr_.contentWindow.postMessage("init", [channel.port2], "*");
  73 +
  74 +
  75 +
  76 + port_.onmessage = function(e){
  77 + self.onmessage(e);
  78 + }
  79 + }
  80 + }
  81 +
  82 + send_ = function(data) {
  83 + if(!!port_ === false) {
  84 + throw("port is null");
  85 + }
  86 + port_.postMessage(data);
  87 + };
  88 +
  89 + onmessage_ = function(e){
  90 + console.log(e);
  91 + };
  92 +
  93 + return {
  94 + "setup": setup_,
  95 + "send" : send_,
  96 + "onmessage" : onmessage_
  97 + }
  98 + }
  99 +})(this)
25 public/p.html
... ... @@ -0,0 +1,25 @@
  1 +<!doctype html>
  2 +<html>
  3 +<head>
  4 +<title>parent</title>
  5 +<script src="javascripts/pipe.js"></script>
  6 +</head>
  7 +<body>
  8 +<h1>HTML5 Messaging</h1>
  9 +<output></output>
  10 +<button>push</button>
  11 +<hr>
  12 +
  13 +<script>
  14 + var client = new Pipe.Client();
  15 + client.setup("c.html");
  16 + client.onmessage = function(e){
  17 + document.querySelector("output").innerText = e.data;
  18 + }
  19 +
  20 + document.querySelector("button").addEventListener("click", function(e){
  21 + client.send(new Date().getTime());
  22 + }, false);
  23 +</script>
  24 +</body>
  25 +</html>
3  routes/index.js
@@ -12,3 +12,6 @@ exports.shorten = function(req, res){
12 12 exports.imageshare = function(req, res){
13 13 res.render('imageshare', { title: 'Image Share(test)' })
14 14 };
  15 +exports.messageport = function(req, res){
  16 + res.render('messageport', { title: 'Message port test' })
  17 +};
8 views/index.ejs
@@ -32,6 +32,10 @@
32 32 <div class=out></div>
33 33 </section>
34 34
  35 +<section id=messageport>
  36 + <button class=event>push event</button>
  37 + <button class=send>send messageport</button>
  38 +</section>
35 39 <hr>
36 40
37 41 <section id="usefullinks">
@@ -59,6 +63,8 @@ $("#urlshortner button").click(function(){
59 63 url);
60 64 window.navigator.startActivity(intent, function(data) {
61 65 $("#urlshortner .out").text(data);
  66 + }, function(e){
  67 + console.log(e);
62 68 });
63 69 })
64 70
@@ -73,6 +79,8 @@ $("#imageshare button").click(function(){
73 79 url);
74 80 window.navigator.startActivity(intent, function(data) {
75 81 $("#imageshare .out").html("<img src='"+data+"'>");
  82 + }, function(e){
  83 + console.log(e);
76 84 });
77 85 });
78 86
6 views/layout.ejs
@@ -15,6 +15,12 @@
15 15 type="image/*"
16 16 href="imageshare"
17 17 title="Image Share(test)" />
  18 + <intent
  19 + action="http://webintents.org/messageport"
  20 + type="event/*"
  21 + href="messageport"
  22 + title="message port test"
  23 + disposition="window" />
18 24 </head>
19 25 <body>
20 26 <%- body %>
27 views/messageport.ejs
... ... @@ -0,0 +1,27 @@
  1 +<h1><%= title %></h1>
  2 +<p>Welcome to <%= title %></p>
  3 +
  4 +<output></output>
  5 +
  6 +<script>
  7 +// obtain intent data
  8 +var obj = window.intent && window.intent.data || null;
  9 +
  10 +$("#shorten button").hide();
  11 +
  12 +if(!!obj === false) {
  13 + alert('obj not specified!!');
  14 +} else {
  15 + console.log(obj);
  16 + alert(obj);
  17 +}
  18 +
  19 +obj[0].onmessage = function(data){
  20 + console.log(data);
  21 + $("output").append(data);
  22 + }
  23 +
  24 +</script>
  25 +
  26 +
  27 +

0 comments on commit faeb21d

Please sign in to comment.
Something went wrong with that request. Please try again.