上一篇我们简单测试了如何快速构建一个聊天程序,虽然很简单很Demo.但是依然摆脱不了B/S的方式去开发.
现在OpWeb[0.0.3]版本给你带来了更令人兴奋的开发方式,全客户端模式.更快速,更便捷的开发模式.
废话不说,直接上index.html
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="../Shared/watch.js" type="text/javascript"></script>
<script src="watch.js" type="text/javascript"></script>
<script src="../Shared/util.js" type="text/javascript"></script>
<script src="util.js" type="text/javascript"></script>
<script src="http://localhost:8000/_comet.io/comet.io.client.js"></script>
<title>easy chat test</title>
</head>
<body>
<div id="content">
</div>
<p>
<input type="text" name="word" id="word" value="" />
</p>
<script>
//define root class
var chatList = function(){
var self = this;
//message list
this.lists = [];
//watch on change
doUIWatch(this,function(prop, action, newval, oldval){
if(action == "push" )
{
//if lists add a item,add a div message in ui
$('#content').append('<div>' + newval[0].message + '</div>');
}
})
};
//new a root item
var chatRoot = new chatList();
//bind item to a sync session
var session1=new opSession(chatRoot,"10000","http://localhost:8000",true);
//if user input on and enter add a new message
$('#word').bind('keydown', function (e) {
var key = e.which;
if (key == 13) {
chatRoot.lists.push({ message : $('#word').val()});
}
});
</script>
</body>
</html>
完了??完了!!!
这是单机版本吧?不信?
你打开2个试试....
DEMO已包含到框架中:
OpWeb框架 : https://github.com/icesun963/OpWeb/
框架介绍:http://blog.csdn.net/icesun963/article/details/17194193