html postMessage 创建聊天应用

应用说明:

这个例子演示如何在门户页面以iframe方式嵌入第三方插件,示例中使用了一个来域名下的应用部件,门户页面通过postMessage来通信。iframe中的聊天部件通过父页面标题内容的闪烁来通知用户。不过,由于部件不在父页面中,而是被隔离在一个来之不同源的页面中,所以部件使用postMessage来修改父页面标题!

1、创建门户页面

<!DOCTYPE>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>postMess</title>
<style>
    iframe{
        width:400px;
        height:300px;
    }
</style>
<script>
    var defaultTitle = ‘Portal[http://localhost]‘;
    var sourse = ‘http://localhost‘;
    var notificationTimer = null;
    function messageHander(e){
        if(e.origin == sourse){
            notify(e.data);
        }else{
            //
        }
    }
    function sendString(s){
        document.getElementById(‘wid‘).contentWindow.postMessage(s,sourse);
    }
    function notify(message){
        stopBinking();
        blinkTitle(message,defaultTitle);
    }
    function stopBinking(){
        if(notificationTimer !== null){
            clearTimeout(notificationTimer);
        }
        document.title = defaultTitle;
    }
    function blinkTitle(m1,m2){
        document.title = m1;
        notificationTimer = setTimeout(blinkTitle,1000,m2,m1);
    }
    function sendStatus(){
        var statusText = document.getElementById(‘statusText‘).value;
        sendString(statusText);
    }
    function loadDemo(){
        document.getElementById(‘sendButton‘).addEventListener(‘click‘,sendStatus,true);
        document.getElementById(‘stopButton‘).addEventListener(‘click‘,stopBinking,true);
        sendStatus();
    }
    window.addEventListener(‘load‘,loadDemo,true);
    window.addEventListener(‘message‘,messageHander,true);
</script>
</head>
<body>

    Status<input type="text" id="statusText" value="online"/>
    <button id="sendButton">change status</button>
    <p>
        <button id="stopButton">stop Binking title</button>
    </p>
    <iframe id="wid" src="get.html"><iframe>
</body>
</html>

2、创建聊天部件页面

<!DOCTYPE>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>postMess</title>
<script type="text/javascript">
    var sourse = ‘http://localhost‘;
    function messageHander(e){
        if(e.origin === sourse){
            document.getElementById(‘status‘).textContent = e.data;
        }else{
            //
        }
    }
    function sendString(s){
        window.top.postMessage(s,sourse);
    }
    function loadDemo(){
        document.getElementById(‘actionButton‘).addEventListener(‘click‘,function(){
            var messText = document.getElementById(‘messText‘).value;
            sendString(messText);
        },true);
    }
    window.addEventListener(‘load‘,loadDemo,true);
    window.addEventListener(‘message‘,messageHander,true);
</script>
</head>
<body>
    status set to:<strong  id="status" /></strong>
    <div>
        <input type="text" id="messText"  value="wid nodd" />
        <button id="actionButton">send nodd</button>
    </div>
</body>
</html>
时间: 2024-08-10 19:01:22

html postMessage 创建聊天应用的相关文章

使用signalR创建聊天室。

浏览器支持Html5的情况下,SignalR使用WebSockets,当不支持时SignalR将使用其它技术来实现通讯. 界面如下:左侧包含三种聊天对象,不同的聊天对象会创建不同的对话框. 设计思路参考:http://www.blue-zero.com/Chat/ 后台代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Microsoft.AspNet.Sig

XMPP - Socket 实现创建聊天室

初始化 @interface ViewController : UIViewController @end 创建输入输出流 @interface ViewController ()<NSStreamDelegate,UITextFieldDelegate,UITableViewDataSource,UITableViewDelegate>{ NSInputStream *_inputStream;//对应输入流 NSOutputStream *_outputStream;//对应输出流 } @

Lync2013 升级错误总结4 创建聊天室失败 提示:服务器无法处理您的请求,请稍候再试

最开始以为是聊天室不支持中文,后来发现是聊天室名称不能与持久聊天类别名称相同 官方说明:

跟我一起使用socket.io创建聊天应用

安装express插件 新建index.js var app = require('express')(); var http = require('http').Server(app); app.get('/', function(req, res){ res.send('<h1>Hello world</h1>'); }); http.listen(9000, function(){ console.log('listening on *:9000'); }); 使用node

【使用SignalR+Asp.net创建实时聊天应用程序】

一.概述: 使用 ASP.NET 那么 SignalR 2 创建一个实时聊天应用程序.将 SignalR 添加 MVC 5 应用程序中,并创建聊天视图发送并显示消息. 在Demo中,将学习SignalR 开发任务包括 ︰ 向 MVC 5 应用程序添加那么 SignalR 图书馆. 创建集线器和浩然启动类,以将内容推送到客户端. 使用 web 页中的那么 SignalR jQuery 库发送邮件并显示更新从集线器. 下面的屏幕快照显示在浏览器中运行的已完成的聊天应用程序. 2.实现: 创建一个 A

使用SignalR+Asp.net创建实时聊天应用程序

一.概述: 使用 ASP.NET 那么 SignalR 2 创建一个实时聊天应用程序.将 SignalR 添加 MVC 5 应用程序中,并创建聊天视图发送并显示消息. 在Demo中,将学习SignalR 开发任务包括 ︰向 MVC 5 应用程序添加那么 SignalR 图书馆.创建集线器和浩然启动类,以将内容推送到客户端.使用 web 页中的那么 SignalR jQuery 库发送邮件并显示更新从集线器. 下面的屏幕快照显示在浏览器中运行的已完成的聊天应用程序. 2.实现: 创建一个 ASP.

MFC: 聊天室创建

1. 创建一个DIALOG的项目 2. 在资源里创建聊天室界面 3. 在CDialog::InitInstance里开头增加: if (!AfxSocketInit()) { AfxMessageBox("load socket liabrary failed!"); return FALSE; } 4. 在stdfax.h里增加: #include <Afxsock.h> 5. 增加成员变量SOCKET m_socket; 6. 增加成员函数BOOL InitSocket

[Asp.net 开发系列之SignalR篇]专题三:使用SignalR实现聊天室的功能

一.引言 在前一篇文章中,我向大家介绍了如何实现实现端对端聊天的功能的,在这一篇文章中将像大家如何使用SignalR实现群聊这样的功能. 二.实现思路 要想实现群聊的功能,首先我们需要创建一个房间,然后每个在线用户可以加入这个房间里面进行群聊,我们可以为房间设置一个唯一的名字来作为标识.那SignalR类库里面是否有这样现有的方法呢?答案是肯定的. // IGroupManager接口提供如下方法 // 作用:将连接ID加入某个组 // Context.ConnectionId 连接ID,每个页

7.持久聊天服务器

服务器准备部分: https://technet.microsoft.com/zh-cn/library/dn951388 https://technet.microsoft.com/zh-cn/library/gg398495 1.服务器基础配置和软件同标准版前端 2.安装 消息队列 3.标准版前端可以并置持久聊天服务器,企业版不可以并置 4.持久聊天服务器的数据存储和合规数据存储,需要SQL数据库支持,测试可以使用后端数据库(RTC实例), 持久聊天服务器角色安装部分: 1.使用拓扑生成器添