当前位置: 首页 > news >正文

制作网站需要怎么做游戏网站设计风格有哪些

制作网站需要怎么做,游戏网站设计风格有哪些,查询数据的网站怎么做,建筑工程网上备案工具#xff1a;Android studio软件方法及协议#xff1a;socket、protobuf实现原理#xff1a;通过本地建立一个socket#xff0c;绑定服务器IP和port#xff0c;然后connect#xff0c;再开启另外线程定时心跳(注意这里的心跳不是自定义发送数据#xff0c;而是采用so…工具Android studio软件方法及协议socket、protobuf实现原理通过本地建立一个socket绑定服务器IP和port然后connect再开启另外线程定时心跳(注意这里的心跳不是自定义发送数据而是采用socket本身的心跳功能sendUrgentData否则有坑)心跳失败则自动重连另一方面启动循环从缓存获取socket数据。大致框架推送实现流程图实现代码public class QpushClient implements Runnable {protected static volatile QpushClient mInstance;//volatile可保证可见性和有序性protected Handler mHandler;protected InetSocketAddress mAddress;String mIMEI;protected String TAG QpushClient;//socket连接的超时时间private final int CONNECT_TIME_OUT 5 * 1000;//巡检周期private final int CHECK_PERIOD 2 * 1000;//连接尝试间隔时间private final int CONNECT_PERIOD 30 * 1000;private final int HEARTBEART_PERIOD 30 * 1000;//若连接失败或响应失败则尝试次数为9若仍无效则不再尝试private final int CONNECT_TRY_TIMES 9;private final int SEND_MSG_TYPE_HEARTBEAT 1; //心跳包private final int SEND_MSG_TYPE_SOCKET_LOGIN 2; //发送socket登录包//连接尝试次数private int mConnectCount;Socket mClientSocket;String mHost;int mPort;//设置是否去读取数据boolean isStartRecieveMsg false;//开启心跳检测boolean isKeepHeartBeat false;BufferedReader mReader;ScheduledExecutorService executor;//定位定时器HeartBeatTask mHeartBeatTask;private QpushClient(Handler handler) {mHandler handler;}public static QpushClient getInstance(Handler handler) {if (mInstance null) {synchronized(QpushClient.class){ //线程安全所以加锁if(mInstance null){mInstance new QpushClient(handler);}}}return mInstance;}public void init(String host, int port,String imei) {mHost host;mPort port;mIMEI imei;new Thread(this).start();isStartRecieveMsg true;isKeepHeartBeat true;}Overridepublic void run() {mAddress new InetSocketAddress(getIP(mHost), mPort);//尝试连接若未连接则设置尝试次数while (mConnectCount CONNECT_TRY_TIMES) {connect();if (!mClientSocket.isConnected()) {mConnectCount;sleep(CONNECT_PERIOD);} else {mConnectCount 0;//连接上则恢复置0break;}}if (mClientSocket.isConnected()) {keepHeartBeat();recvProtobufMsg();// recvStringMsg();}}private void connect() {try {if (mClientSocket null) {mClientSocket new Socket();}mClientSocket.connect(mAddress, CONNECT_TIME_OUT);Driver.HeartBeat heartbeat Driver.HeartBeat.newBuilder().setImei(mIMEI).build();Driver.ClientMessage socketLogin Driver.ClientMessage.newBuilder().setType(1).setHeartBeat(heartbeat).build();sendMsg(socketLogin,SEND_MSG_TYPE_SOCKET_LOGIN);} catch (IOException e) {e.printStackTrace();Log.e(TAG, 连接失败 mClientSocket.connect fail ,ip mAddress.getHostName() ;port mAddress.getPort() ;detail: e.getMessage());}}/*** 心跳维护*/private void keepHeartBeat() {//设置心跳频率启动心跳if (isKeepHeartBeat) {if (mHeartBeatTask null) {mHeartBeatTask new HeartBeatTask();}try {if (executor ! null) {executor.shutdownNow();executor null;}executor Executors.newScheduledThreadPool(1);executor.scheduleAtFixedRate(mHeartBeatTask,1000, //initDelayHEARTBEART_PERIOD, //periodTimeUnit.MILLISECONDS);} catch (Exception ex) {ex.printStackTrace();}}}/*** param message* param type 1login2心跳*/public void sendMsg(String message, int type) {PrintWriter writer;try {writer new PrintWriter(new OutputStreamWriter(mClientSocket.getOutputStream(), UTF-8), true);writer.println(message);Log.e(TAG, sendMsg Socket.isClosed() mClientSocket.isClosed() ;connect mClientSocket.isConnected());} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();switch (type) {case SEND_MSG_TYPE_HEARTBEAT:mHandler.obtainMessage(QpushService.PUSH_TYPE_PROTO_DATA, 发送心跳异常).sendToTarget();break;}}}/*** param message* param type 1login2心跳*/public void sendMsg(Driver.ClientMessage message, int type) {try {message.writeTo(mClientSocket.getOutputStream());Log.e(TAG, sendMsg success);} catch (IOException e) {// TODO Auto-generated catch blockif (type SEND_MSG_TYPE_HEARTBEAT) {//心跳失败Log.e(TAG, 心跳失败);if (mClientSocket.isClosed()) {connect();}} else {Log.e(TAG, 发送数据失败);}e.printStackTrace();}}/*** 不断的检测是否有服务器推送的数据过来*/public void recvStringMsg() {while (mClientSocket ! null mClientSocket.isConnected() !mClientSocket.isClosed()) {try {mReader new BufferedReader(new InputStreamReader(mClientSocket.getInputStream(), UTF-8));String data mReader.readLine();Log.e(TAG, recvStringMsg data data);} catch (IOException e) {e.printStackTrace();} catch (Exception ex) {ex.printStackTrace();}sleep(2000);}sleep(CHECK_PERIOD);}/*** 不断的检测是否有服务器推送的数据过来*/public void recvProtobufMsg() {while (isStartRecieveMsg) {try {byte[] resultByte recvByteMsg(mClientSocket.getInputStream());if (resultByte ! null) {Driver.ClientMessage retMsg Driver.ClientMessage.parseFrom(resultByte);mHandler.obtainMessage(QpushService.PUSH_TYPE_PROTO_DATA, retMsg).sendToTarget();} else {Log.e(TAG, resultByte is null);}} catch (IOException e) {e.printStackTrace();} catch (Exception ex) {ex.printStackTrace();}sleep(5 * 1000);}}/*** 接收server的信息** return*/public byte[] recvByteMsg(InputStream inpustream) {try {byte len[] new byte[1024];int count inpustream.read(len);byte[] temp new byte[count];for (int i 0; i count; i) {temp[i] len[i];}return temp;} catch (Exception localException) {localException.printStackTrace();}return null;}class HeartBeatTask implements Runnable {Overridepublic void run() {//执行发送心跳try {mClientSocket.sendUrgentData(65);} catch (IOException e) {e.printStackTrace();try {Log.e(TAG, socket心跳异常尝试断开重连);mClientSocket.close();mClientSocket null;//然后尝试重连connect();} catch (IOException e1) {e1.printStackTrace();}}Log.e(TAG, 发送心跳Socket.isClosed() mClientSocket.isClosed() ;connect mClientSocket.isConnected());}}/*** 通过域名获取IP** param domain* return*/public String getIP(String domain) {String IPAddress ;InetAddress ReturnStr1 null;try {ReturnStr1 java.net.InetAddress.getByName(domain);IPAddress ReturnStr1.getHostAddress();} catch (UnknownHostException e) {e.printStackTrace();Log.e(TAG, 获取IP失败 e.getMessage());}return IPAddress;// return 192.168.3.121;}private void sleep(long sleepTime) {try {Thread.sleep(sleepTime);} catch (InterruptedException e) {e.printStackTrace();}}/*** 销毁socket*/public void onDestory() {if (mClientSocket ! null) {try {mClientSocket.close();} catch (IOException e) {e.printStackTrace();}mClientSocket null;}}/** Ready for use.*/public void close() {try {if (mClientSocket ! null !mClientSocket.isClosed())mClientSocket.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}QpushService类:主要负责启动socket客户端及管理消息public class QpushService extends Service {//数据推送不显示到通知栏final static int PUSH_TYPE_DATA 1;final static int PUSH_TYPE_PROTO_DATA 2;Handler mHandler;String TAG QpushService;QpushClient mClient;Overridepublic void onCreate() {Log.e(TAG, onCreate);initHandler();super.onCreate();}/*** 初始化handler用于接收推送的消息*/private void initHandler() {mHandler new Handler() {Overridepublic void handleMessage(Message msg) {switch (msg.what) {case PUSH_TYPE_DATA:Log.e(TAG, PUSH_TYPE_DATA);String data (String) msg.obj;ToastUtil.showShort(QpushService.this.getApplicationContext(), data);case PUSH_TYPE_PROTO_DATA:Driver.ClientMessage clientMessage (Driver.ClientMessage) msg.obj;Log.e(TAG, PUSH_TYPE_PROTO_DATA);Intent intentCancelRighr new Intent();intentCancelRighr.setAction(PushReceiverAction.PUSH_ACTION);intentCancelRighr.putExtra(data, clientMessage);getApplicationContext().sendBroadcast(intentCancelRighr);break;}}};}NullableOverridepublic IBinder onBind(Intent intent) {Log.e(TAG, onBind);return null;}Overridepublic int onStartCommand(Intent intent, int flags, int startId) {mClient QpushClient.getInstance(mHandler);mClient.init(HttpUrls.SOCKET_HOST, HttpUrls.SOCKET_PORT, AppUtils.getPesudoUniqueID());return super.onStartCommand(intent, flags, startId);}Overridepublic void onDestroy() {Log.e(TAG, onDestroy);super.onDestroy();mClient.onDestory();}}PushReceiver类接收service消息的广播运行的主进程防止接收不到数据public class PushReceiver extends BroadcastReceiver {Context mContext;String TAG PushReceiver;Overridepublic void onReceive(Context context, Intent intent) {mContext context;if (intent.getAction().equals(PushReceiverAction.PUSH_ACTION)) {//推送的通知消息Driver.ClientMessage clientMessage (Driver.ClientMessage) intent.getSerializableExtra(data);if (clientMessage ! null) {switch (clientMessage.getType()) {case 1: //socket登录不做处理Log.e(TAG, socket登录消息,sid clientMessage.getHeartBeat().getSid());MyApplication.sid clientMessage.getHeartBeat().getSid();// showNotification(socket,登录成功,1);break;case 2://通知告知被迫取消领取红包资格Log.e(TAG, 取消抢红包资格,title clientMessage.getLogOut().getTitle() ;message clientMessage.getLogOut().getContent());ToastUtil.showShort(mContext, 被迫取消抢红包资格);Intent intentCancelRighr new Intent();intentCancelRighr.setAction(PushReceiverAction.CANCEL_REDPACKET_RIGHT);intentCancelRighr.putExtra(data, clientMessage);context.sendBroadcast(intentCancelRighr);MyApplication.mLoginStatus 1;showNotification(clientMessage.getLogOut().getTitle(), clientMessage.getLogOut().getContent(), 1);break;case 3: //领取红包/未领到红包消息Intent intentGo new Intent();intentGo.setAction(clientMessage.getRedPacket().getResult() 1 ? PushReceiverAction.GET_REDPACKET_ACTION : PushReceiverAction.DONT_GET_REDPACKET_ACTION);intentGo.putExtra(data, clientMessage);context.sendBroadcast(intentGo);Log.e(TAG, 红包消息content clientMessage.getRedPacket().getContent());showNotification(clientMessage.getRedPacket().getTitle(), clientMessage.getRedPacket().getContent(), 2);break;}} else {Log.e(TAG, clientMessage is null);}}}/*** 在状态栏显示通知*/SuppressWarnings(deprecation)private void showNotification(String title, String content, int type) {// 创建一个NotificationManager的引用NotificationManager notificationManager (NotificationManager) mContext.getSystemService(android.content.Context.NOTIFICATION_SERVICE);NotificationCompat.Builder builder new NotificationCompat.Builder(mContext);builder.setContentTitle(title).setContentText(content).setTicker(title)// .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))//点击通知栏设置意图.setWhen(System.currentTimeMillis()).setPriority(Notification.PRIORITY_HIGH)// .setAutoCancel(true).setOngoing(false)//ture设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接).setSmallIcon(R.drawable.logo);Notification notification builder.build();Log.e(TAG, isSoundOn: SharePreferUtils.isSoundOn(mContext));if (SharePreferUtils.isSoundOn(mContext) type 2) { //type红包消息notification.sound Uri.parse(android.resource:// mContext.getPackageName() / R.raw.diaoluo_da);}Intent notificationIntent;if (type 2) {//进入我的资产notificationIntent HomeActivity.toWitchFragmentOfHome(mContext, Constants.TOWITCHOFHOME_MYASSETS);} else {//进入主页流量球notificationIntent HomeActivity.toWitchFragmentOfHome(mContext, Constants.TOWITCHOFHOME_BALL);}Log.e(TAG, type type);// 点击该通知后要跳转的ActivityPendingIntent contentItent PendingIntent.getActivity(mContext, 0, notificationIntent, 0);notification.contentIntent contentItent;notification.flags Notification.FLAG_AUTO_CANCEL;// 把Notification传递给NotificationManagernotificationManager.notify(type, notification);}}Driver类该类是通过proto编译出来的代码量比较大Driver.proto文件贴下来Driver.java文件我附上下载链接编译教程见protobuf在Android推送中的使用方法(比json、xml都要好的数据结构)Driver.proto文件syntax proto3;//编译教程http://www.jianshu.com/p/8036003cb849message ClientMessage{int32 type 1; //类型1socket登录;2取消抢红包资格;3领取红包消息HeartBeat heartBeat 2; //socket登录LogOut logOut 3; //取消抢红包资格消息RedPacket redPacket 4; //领取未抢到红包消息}message HeartBeat{string sid 1; //服务IDstring imei 2; //手机唯一编号imei}message LogOut{string sid 1; //服务IDint32 result 2; //1成功2失败string title 3; //消息标题string content 4; //消息内容}message RedPacket{string sid 1; //服务IDint32 result 2; //1成功2失败int32 price 3; //红包金额单位分string title 4; //消息标题string content 5; //消息内容}
http://www.zqtcl.cn/news/738614/

相关文章:

  • 做棋牌游戏网站犯法吗如何进行搜索引擎的优化
  • 常见的网站首页布局有哪几种陈光锋网站运营推广新动向
  • 手机网站活动策划方案开一个设计公司
  • 宝塔建设网站教程visual studio 2010 网站开发教程
  • 做网站购买服务器做谷歌网站使用什么统计代码吗
  • 网站系统与网站源码的关系emlog轻松转wordpress
  • 网站的简介怎么在后台炒做吉林省住房城乡建设厅网站首页
  • 泉州易尔通网站建设国际酒店网站建设不好
  • 网页下载网站福田企业网站推广公司
  • 北京网站建设开发公司哪家好网站添加在线留言
  • 新建的网站怎么做seo优化平面广告创意设计
  • yy陪玩网站怎么做软件项目管理计划
  • 西安建网站价格低百度推广区域代理
  • 中英网站模板 照明公司注册在自贸区的利弊
  • 全球十大网站排名wordpress标题连接符
  • 网站开发可能遇到的问题四川建筑人才招聘网
  • 镇江网站托管怎么做淘宝网站赚钱吗
  • 交互式网站是什么知名vi设计企业
  • 上海个人做网站网站建设销售好做嘛
  • 邵阳建设网站哪家好手机网站栏目结构图
  • 做动车哪个网站查网站环境配置
  • 那些网站可以做h5国内新闻最新消息今天简短
  • asp网站开发实例河南省建设招投标网站
  • 营销型网站搭建公司有没有专做推广小说的网站
  • 汕头网站搭建wordpress文章列表摘要
  • 网站开发体会800字网站开发新功能
  • 网站域名查询ip杭州pc网站开发公司有哪些
  • 青岛公司网站设计网站后台编辑器内容不显示
  • vc6.0做网站wordpress调用会员等级
  • 哪个网站有做商标网站的类型是什么意思