Permalink
Browse files

merged chat

  • Loading branch information...
2 parents 42a1c6f + bc575f9 commit faeb21d44f630621bc54ef9b9ae9bd8dd3b115f4 @KensakuKOMATSU committed Jan 16, 2012
Showing with 196 additions and 2 deletions.
  1. +3 −2 app.js
  2. +25 −0 public/c.html
  3. +99 −0 public/javascripts/pipe.js
  4. +25 −0 public/p.html
  5. +3 −0 routes/index.js
  6. +8 −0 views/index.ejs
  7. +6 −0 views/layout.ejs
  8. +27 −0 views/messageport.ejs
View
5 app.js
@@ -20,18 +20,19 @@ app.configure(function(){
});
app.configure('development', function(){
- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
- app.use(express.errorHandler());
+ app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
app.get('/shorten', routes.shorten);
app.get('/imageshare', routes.imageshare);
+app.get('/messageport', routes.messageport);
app.listen(process.env.PORT || process.env.VMC_APP_PORT || 3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
View
25 public/c.html
@@ -0,0 +1,25 @@
+<!doctype html>
+<html>
+<head>
+<title>child</title>
+<script src="javascripts/pipe.js"></script>
+</head>
+<body>
+Hello, child!!
+<output></output>
+
+<script>
+ var server = new Pipe.Server();
+ server.setup();
+
+ setInterval(function(){
+ server.send(new Date().getTime());
+ }, 1000);
+
+ server.onmessage = function(e) {
+ document.querySelector("output").innerText = e.data;
+ this.send("recv: "+e.data);
+ }
+</script>
+</body>
+</html>
View
99 public/javascripts/pipe.js
@@ -0,0 +1,99 @@
+var Pipe = {};
+
+(function(global){
+ Pipe.Server = function(){
+ var setup_, send_, onmessage_, onconnect_, port_, flag = true;
+
+ send_ = function(data) {
+ if(!!port_ === false) {
+ throw("connection not established");
+ }
+
+ port_.postMessage(data);
+ };
+
+ onmessage_ = function(data){
+ console.log("Pipe.Server.onmessage");
+ console.log(data);
+ };
+
+ onconnect_ = function(data){
+ console.log("Pipe.Server.onconnect");
+ console.log("connection established");
+ };
+
+
+ setup_ = function(){
+ var self = this;
+ window.addEventListener("message", function(e) {
+ if(flag) {
+ self.onconnect(e);
+
+ port_ = e.ports[0];
+ flag = false;
+
+ port_.onmessage = function(e){
+ self.onmessage(e);
+ }
+ }
+ }, false);
+ }
+
+ return {
+ "setup": setup_,
+ "onconnect" : onconnect_,
+ "send" : send_,
+ "onmessage" : onmessage_
+ }
+ }
+
+ Pipe.Client = function(){
+ var setup_, send_, onmessage_, port_, ifr_, flag = true;
+
+
+ setup_ = function(url, id){
+ // [TODO] validates url string
+ if(typeof(url) !== "string") {
+ throw("parameter url error");
+ }
+
+ ifr_ = document.createElement('iframe');
+ ifr_.src = url;
+ document.body.appendChild(ifr_);
+
+ var self = this;
+
+ ifr_.onload = function(e){
+ var channel = new MessageChannel();
+ port_ = channel.port1;
+ console.log("onload iframe");
+
+ // [TODO] passes origin
+ ifr_.contentWindow.postMessage("init", [channel.port2], "*");
+
+
+
+ port_.onmessage = function(e){
+ self.onmessage(e);
+ }
+ }
+ }
+
+ send_ = function(data) {
+ if(!!port_ === false) {
+ throw("port is null");
+ }
+ port_.postMessage(data);
+ };
+
+ onmessage_ = function(e){
+ console.log(e);
+ };
+
+ return {
+ "setup": setup_,
+ "send" : send_,
+ "onmessage" : onmessage_
+ }
+ }
+})(this)
View
25 public/p.html
@@ -0,0 +1,25 @@
+<!doctype html>
+<html>
+<head>
+<title>parent</title>
+<script src="javascripts/pipe.js"></script>
+</head>
+<body>
+<h1>HTML5 Messaging</h1>
+<output></output>
+<button>push</button>
+<hr>
+
+<script>
+ var client = new Pipe.Client();
+ client.setup("c.html");
+ client.onmessage = function(e){
+ document.querySelector("output").innerText = e.data;
+ }
+
+ document.querySelector("button").addEventListener("click", function(e){
+ client.send(new Date().getTime());
+ }, false);
+</script>
+</body>
+</html>
View
3 routes/index.js
@@ -12,3 +12,6 @@ exports.shorten = function(req, res){
exports.imageshare = function(req, res){
res.render('imageshare', { title: 'Image Share(test)' })
};
+exports.messageport = function(req, res){
+ res.render('messageport', { title: 'Message port test' })
+};
View
8 views/index.ejs
@@ -32,6 +32,10 @@
<div class=out></div>
</section>
+<section id=messageport>
+ <button class=event>push event</button>
+ <button class=send>send messageport</button>
+</section>
<hr>
<section id="usefullinks">
@@ -59,6 +63,8 @@ $("#urlshortner button").click(function(){
url);
window.navigator.startActivity(intent, function(data) {
$("#urlshortner .out").text(data);
+ }, function(e){
+ console.log(e);
});
})
@@ -73,6 +79,8 @@ $("#imageshare button").click(function(){
url);
window.navigator.startActivity(intent, function(data) {
$("#imageshare .out").html("<img src='"+data+"'>");
+ }, function(e){
+ console.log(e);
});
});
View
6 views/layout.ejs
@@ -15,6 +15,12 @@
type="image/*"
href="imageshare"
title="Image Share(test)" />
+ <intent
+ action="http://webintents.org/messageport"
+ type="event/*"
+ href="messageport"
+ title="message port test"
+ disposition="window" />
</head>
<body>
<%- body %>
View
27 views/messageport.ejs
@@ -0,0 +1,27 @@
+<h1><%= title %></h1>
+<p>Welcome to <%= title %></p>
+
+<output></output>
+
+<script>
+// obtain intent data
+var obj = window.intent && window.intent.data || null;
+
+$("#shorten button").hide();
+
+if(!!obj === false) {
+ alert('obj not specified!!');
+} else {
+ console.log(obj);
+ alert(obj);
+}
+
+obj[0].onmessage = function(data){
+ console.log(data);
+ $("output").append(data);
+ }
+
+</script>
+
+
+

0 comments on commit faeb21d

Please sign in to comment.