|
@@ -0,0 +1,46 @@
|
|
|
|
|
+<html>
|
|
|
|
|
+<head>
|
|
|
|
|
+ <title>Share Object</title>
|
|
|
|
|
+ <!--从服务器获取70K大小的兼容ie5+等浏览器、falsh、支持websocket的浏览器,不知道他是怎么判断的,以后可以深入研究-->
|
|
|
|
|
+ <script src="http://nodejs.1473.cn:888/socket.io/socket.io.js" type="text/javascript"></script>
|
|
|
|
|
+ <script src="http://www.1473.cn/js/uc/U.M.js" type="text/javascript"></script>
|
|
|
|
|
+ <script src="http://www.1473.cn/js/uc/u.extend.js" type="text/javascript"></script>
|
|
|
|
|
+ <style type="text/css">
|
|
|
|
|
+ #moveobj { position:absolute; width: 100px; height: 70px; background: red; }
|
|
|
|
|
+ </style>
|
|
|
|
|
+ <script type="text/javascript">
|
|
|
|
|
+ //连接socketio服务器
|
|
|
|
|
+ var socket = io.connect('http://localhost:888');
|
|
|
|
|
+ function startsocket() {
|
|
|
|
|
+ //监听服务端发送来的数据,在页面上展示效果
|
|
|
|
|
+ socket.on('bPosition', function (data) {
|
|
|
|
|
+ setPosition(data.position,data.xinfo);//根据服务信息更新整个界面。
|
|
|
|
|
+ });
|
|
|
|
|
+ //连接socketio成功信息
|
|
|
|
|
+ socket.on('connect', function () {info('成功连接sockeio.');});
|
|
|
|
|
+ }
|
|
|
|
|
+ function animate() {
|
|
|
|
|
+ var obj = $("#moveobj")[0];//得到对象。
|
|
|
|
|
+ var _x = parseInt(obj.style.left);//得到左坐标
|
|
|
|
|
+ obj.style.left = _x + 1 + "px";//客户端移动
|
|
|
|
|
+ //info(_x); //调试信息
|
|
|
|
|
+ //给服务器发送自己的位置。
|
|
|
|
|
+ socket.emit('cPosition', { position: _x,xinfo:_x });
|
|
|
|
|
+ setTimeout("animate()", 100);//计时器刷新
|
|
|
|
|
+ };
|
|
|
|
|
+ function info(str) { $("#info")[0].innerHTML += "X坐标:" + str + "</br>"; }
|
|
|
|
|
+ function setPosition(position, xinfo) { $("#moveobj")[0].style.left = position; $("#info")[0].innerHTML += "X坐标:" + xinfo + "</br>"; }
|
|
|
|
|
+ window.onload = startsocket;
|
|
|
|
|
+ </script>
|
|
|
|
|
+
|
|
|
|
|
+</head>
|
|
|
|
|
+<body>
|
|
|
|
|
+ <h1>
|
|
|
|
|
+ 游戏即时通讯示例</h1>
|
|
|
|
|
+ <input value="开始" type=button onclick="animate()" />
|
|
|
|
|
+ <div id="moveobj" style=" top:100px; left:100px;">
|
|
|
|
|
+ Drag me</div>
|
|
|
|
|
+ <div id='info' style=" position:absolute; top:210px; left:100px; width:1000px; height:1000px;">
|
|
|
|
|
+ </div>
|
|
|
|
|
+</body>
|
|
|
|
|
+</html>
|