亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android利用websocket協(xié)議與服務(wù)器通信

 更新時間:2020年04月05日 08:32:59   作者:成電小菜  
這篇文章主要為大家詳細(xì)介紹了android利用websocket協(xié)議與服務(wù)器通信,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近做一個項目,需求中需要服務(wù)器主動推送消息到客戶端。這樣的話一般的http連接就不能使用了。博主問了個朋友,向我推薦websocket協(xié)議,特此測試了一下,發(fā)現(xiàn)效果很好。

android本身沒有websocket的庫,需要自己下載 ,下載地址

客戶端代碼:

界面布局自己寫,很簡單的兩個button

package com.example.test; 
 
import com.example.test.R; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
import de.tavendo.autobahn.WebSocketConnection; 
import de.tavendo.autobahn.WebSocketConnectionHandler; 
import de.tavendo.autobahn.WebSocketException; 
 
public class MainActivity extends Activity implements OnClickListener { 
 
 private Button bt; 
 private EditText ed_name; 
 private EditText ed_text; 
 private Button bt1; 
 WebSocketConnection wsc; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 bt = (Button) findViewById(R.id.bt); 
 ed_name = (EditText) findViewById(R.id.ed_name); 
 ed_text = (EditText) findViewById(R.id.ed_text); 
 bt1 = (Button) findViewById(R.id.bt1); 
 bt.setOnClickListener(this); 
 bt1.setOnClickListener(this); 
 wsc = new WebSocketConnection(); 
 
 } 
 
 private void connect() { 
 System.out.println("開始連接websocket///"); 
 
 try { 
 
  wsc.connect("ws://192.168.1.245:8080/DriveServer/mainservlet", 
   new WebSocketConnectionHandler() { 
   
   @Override 
   public void onBinaryMessage(byte[] payload) { 
    System.out.println("onBinaryMessage size=" 
     + payload.length); 
   } 
 
   @Override 
   public void onClose(int code, String reason) { 
    System.out.println("onClose reason=" + reason); 
   } 
 
   @Override 
   public void onOpen() { 
    System.out.println("onOpen"); 
    showtext("連接成功"); 
    // wsc.sendTextMessage("Hello!"); 
    // wsc.disconnect(); 
   } 
   @Override 
   public void onRawTextMessage(byte[] payload) { 
    System.out.println("onRawTextMessage size=" 
     + payload.length); 
   } 
 
   @Override 
   public void onTextMessage(String payload) { 
    System.out.println("onTextMessage" + payload); 
    showtext(payload); 
   } 
 
   }); 
 } catch (WebSocketException e) { 
  // TODO Auto-generated catch block 
  e.printStackTrace(); 
 } 
 
 } 
 @Override 
 public void onClick(View v) { 
 // TODO Auto-generated method stub 
 int id = v.getId(); 
 switch (id) { 
  case R.id.bt : 
  wsc.sendTextMessage("我是客戶端,我通過ws往服務(wù)器發(fā)數(shù)據(jù)"); 
  break; 
  case R.id.bt1 : 
  connect(); 
  break; 
  default : 
  break; 
 } 
 } 
 private void showtext(String msg) { 
 Toast.makeText(this, msg, 0).show(); 
 } 
} 

下面是服務(wù)器代碼:

用的是jetty7自帶的websocket庫

package com.websocket; 
 
import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.nio.ByteBuffer; 
import java.nio.CharBuffer; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.Set; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
import org.eclipse.jetty.websocket.WebSocket; 
import org.eclipse.jetty.websocket.WebSocket.OnTextMessage; 
import org.eclipse.jetty.websocket.WebSocketServlet; 
 
public class ChatWebSocketServlet extends WebSocketServlet { 
 
 private static final long serialVersionUID = 911879078000755859L; 
 
 private List<MyWebSocket> list = new ArrayList<MyWebSocket>(); 
 
 class MyWebSocket implements OnTextMessage { 
 private String userName = "匿名用戶"; 
 
 public MyWebSocket(String userName) { 
  if (userName != null && userName.length() > 0) { 
  this.userName = userName; 
  } 
 } 
 
 private Connection conn; 
 
 public void onClose(int arg0, String arg1) { 
  // TODO Auto-generated method stub 
  System.out.println("onOpen 斷開連接了。。。。。。。。" + arg1 + " " + arg0); 
  list.remove(this); 
 } 
 
 public void onOpen(Connection arg0) { 
  // TODO Auto-generated method stub 
  System.out 
   .println("onOpen 連接了。。。。。。。。。。。。" + arg0.getMaxIdleTime()); 
  this.conn = arg0; 
  
  if (!list.contains(arg0)) { 
  list.add(this); 
  } else { 
  System.out.println("這個用戶已經(jīng)連接了"); 
  } 
  System.out.println(".." + list.size()); 
 } 
 
 public void onMessage(String arg0) { 
  
  String toname = arg0.substring(0, 3); 
  System.out.println("toname-->" + toname); 
  try { 
   
  list.get(0).getConn() 
   .sendMessage(arg0.substring(3)); 
  System.out.println("發(fā)送的數(shù)據(jù)" + arg0.substring(3)); 
  } catch (IOException e) { 
  // TODO Auto-generated catch block 
  e.printStackTrace(); 
  } 
 } 
 
 private Connection getConn() { 
  return this.conn; 
 } 
 } 
 
 public WebSocket doWebSocketConnect(HttpServletRequest arg0, String arg1) { 
 // TODO Auto-generated method stub 
 String name = ""; 
 name = arg0.getParameter("username"); 
 System.out.println(name + "is connected"); 
 return new MyWebSocket(name); 
 } 
 
 @Override 
 protected void service(HttpServletRequest request, 
  HttpServletResponse response) throws ServletException, IOException { 
 // TODO Auto-generated method stub 
 super.service(request, response); 
 } 
 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論