WebSocket工具类
最近的项目在整长连接WebSocket,之前也写过一个感觉没有这个全面。提供个工具类WebSocketHelper和Java-WebSocket-1.3.9.jar包以及一个HttpURLConnectionUtil
1、WebSocketHelper
-
import android.util.Log;
-
-
import org.java_websocket.client.WebSocketClient;
-
import org.java_websocket.drafts.Draft;
-
import org.java_websocket.drafts.Draft_6455;
-
import org.java_websocket.handshake.ServerHandshake;
-
-
import java.net.URI;
-
import java.util.Date;
-
import java.util.concurrent.TimeUnit;
-
-
/**
-
* author : jsxin
-
* e-mail : jsxin0816@163.com
-
* time : 2023/07/03
-
* desc : WebSocketHelper工具类
-
*/
-
public class WebSocketHelper {
-
public static final String TAG = WebSocketHelper.class.getSimpleName();
-
private RobotWebSocket ws_robot;
-
private boolean isRunning;
-
private boolean isInterrupt;
-
public static boolean isOpenOk = false;
-
-
private WebSocketHelper() {
-
this.isRunning = false;
-
this.isInterrupt = false;
-
}
-
-
public static WebSocketHelper getInstance() {
-
return SingletonHolder.instance;
-
}
-
-
public boolean isRunning() {
-
return this.isRunning;
-
}
-
-
public void ConnectService(final String serive_ip, final String pad_name, final WebSokcetCallback cb) {
-
if (!this.isRunning) {
-
this.isInterrupt = false;
-
Runnable runnable = new Runnable() {
-
public void run() {
-
while(!WebSocketHelper.this.isInterrupt) {
-
WebSocketHelper.this.isRunning = true;
-
if (WebSocketHelper.isOpenOk) {
-
try {
-
Thread.sleep(2000L);
-
} catch (Exception var2) {
-
var2.printStackTrace();
-
}
-
} else {
-
try {
-
String url = "ws://" serive_ip "/webSocket/" 1;
-
System.out.print(url);
-
Log.i(WebSocketHelper.TAG, url);
-
WebSocketHelper.this.ws_robot = WebSocketHelper.this.new RobotWebSocket(new URI(url), new Draft_6455(), cb);
-
WebSocketHelper.this.ws_robot.connectBlocking(10L, TimeUnit.SECONDS);
-
} catch (Exception var4) {
-
Log.e(WebSocketHelper.TAG, var4.getMessage());
-
}
-
-
try {
-
Thread.sleep(2000L);
-
} catch (Exception var3) {
-
var3.printStackTrace();
-
}
-
}
-
}
-
-
WebSocketHelper.this.isRunning = false;
-
Log.i(WebSocketHelper.TAG, "robot websocket run over");
-
}
-
};
-
Thread thread = new Thread(runnable);
-
thread.start();
-
}
-
}
-
-
public int sendMsg(String message) {
-
Log.d(TAG, "message==" message);
-
synchronized(this.ws_robot) {
-
try {
-
if (this.ws_robot != null) {
-
this.ws_robot.send(message);
-
}
-
} catch (Exception var5) {
-
var5.printStackTrace();
-
isOpenOk = false;
-
return 1;
-
}
-
-
return 0;
-
}
-
}
-
-
public void destroy() {
-
try {
-
this.isInterrupt = true;
-
if (this.ws_robot != null) {
-
this.ws_robot.closeBlocking();
-
}
-
} catch (Exception var2) {
-
var2.printStackTrace();
-
}
-
-
}
-
-
private class RobotWebSocket extends WebSocketClient {
-
private WebSokcetCallback cb_;
-
-
RobotWebSocket(URI serverUri, Draft protocolDraft, WebSokcetCallback cb) {
-
super(serverUri);
-
this.cb_ = cb;
-
}
-
-
public void onOpen(ServerHandshake handshakedata) {
-
Log.i(WebSocketHelper.TAG, "onOpen");
-
WebSocketHelper.isOpenOk = true;
-
if (this.cb_ != null) {
-
this.cb_.onConnect();
-
}
-
-
}
-
-
public void onMessage(String message) {
-
Log.d(WebSocketHelper.TAG, "receive: " message);
-
if (this.cb_ != null) {
-
this.cb_.onMessageData(message);
-
}
-
-
}
-
-
public void onClose(int code, String reason, boolean remote) {
-
Log.i(WebSocketHelper.TAG, "Connection close by " (remote ? "remote peer" : "us") " at " new Date(System.currentTimeMillis()));
-
WebSocketHelper.isOpenOk = false;
-
if (this.cb_ != null) {
-
this.cb_.onDisonnect();
-
}
-
-
}
-
-
public void onError(Exception ex) {
-
Log.e(WebSocketHelper.TAG, "onError" ex.getMessage());
-
WebSocketHelper.isOpenOk = false;
-
if (this.cb_ != null) {
-
this.cb_.onDisonnect();
-
}
-
-
}
-
}
-
-
public interface WebSokcetCallback {
-
void onConnect();
-
-
void onDisonnect();
-
-
void onMessageData(String var1);
-
}
-
-
private static class SingletonHolder {
-
private static final WebSocketHelper instance = new WebSocketHelper();
-
-
private SingletonHolder() {
-
}
-
}
-
}
2、HttpURLConnectionUtil
-
import android.support.annotation.Nullable;
-
import android.util.Log;
-
-
import java.io.BufferedReader;
-
import java.io.IOException;
-
import java.io.InputStream;
-
import java.io.InputStreamReader;
-
import java.io.OutputStream;
-
import java.net.HttpURLConnection;
-
import java.net.MalformedURLException;
-
import java.net.URL;
-
-
/**
-
* author : jsxin
-
* e-mail : jsxin0816@163.com
-
* time : 2023/07/24
-
* desc :
-
*/
-
public class HttpURLConnectionUtil {
-
public static final String TAG = HttpURLConnectionUtil.class.getSimpleName();
-
-
public HttpURLConnectionUtil() {
-
}
-
-
public static String doGet(String httpUrl) {
-
HttpURLConnection connection = null;
-
InputStream is = null;
-
BufferedReader br = null;
-
StringBuffer result = new StringBuffer();
-
-
try {
-
URL url = new URL(httpUrl);
-
connection = (HttpURLConnection)url.openConnection();
-
connection.setRequestMethod("GET");
-
connection.setReadTimeout(15000);
-
connection.connect();
-
if (connection.getResponseCode() == 200) {
-
is = connection.getInputStream();
-
if (null != is) {
-
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
-
String temp = null;
-
-
while(null != (temp = br.readLine())) {
-
result.append(temp);
-
}
-
}
-
}
-
} catch (IOException var19) {
-
var19.printStackTrace();
-
} finally {
-
if (null != br) {
-
try {
-
br.close();
-
} catch (IOException var18) {
-
var18.printStackTrace();
-
}
-
}
-
-
if (null != is) {
-
try {
-
is.close();
-
} catch (IOException var17) {
-
var17.printStackTrace();
-
}
-
}
-
-
connection.disconnect();
-
}
-
-
return result.toString();
-
}
-
-
public static String doPost(String httpUrl, { String param)
-
StringBuffer result = new StringBuffer();
-
HttpURLConnection connection = null;
-
OutputStream os = null;
-
InputStream is = null;
-
BufferedReader br = null;
-
-
try {
-
URL url = new URL(httpUrl);
-
connection = (HttpURLConnection)url.openConnection();
-
connection.setRequestMethod("POST");
-
connection.setConnectTimeout(15000);
-
connection.setReadTimeout(15000);
-
connection.setUseCaches(false);
-
connection.setDoOutput(true);
-
connection.setDoInput(true);
-
connection.setRequestProperty("accept", "*/*");
-
connection.setRequestProperty("connection", "Keep-Alive");
-
connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
-
connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36)");
-
if (null != param && !param.equals("")) {
-
os = connection.getOutputStream();
-
os.write(param.getBytes("UTF-8"));
-
os.flush();
-
}
-
-
int httpstatus = connection.getResponseCode();
-
Log.i(TAG, "http status :" httpstatus);
-
if (httpstatus == 200) {
-
is = connection.getInputStream();
-
if (null != is) {
-
br = new BufferedReader(new InputStreamReader(is, "utf-8"));
-
String temp = null;
-
-
while(null != (temp = br.readLine())) {
-
result.append(temp);
-
result.append("\r\n");
-
}
-
}
-
}
-
} catch (MalformedURLException var30) {
-
var30.printStackTrace();
-
} catch (IOException var31) {
-
var31.printStackTrace();
-
} finally {
-
if (br != null) {
-
try {
-
br.close();
-
} catch (IOException var29) {
-
var29.printStackTrace();
-
}
-
}
-
-
if (os != null) {
-
try {
-
os.close();
-
} catch (IOException var28) {
-
var28.printStackTrace();
-
}
-
}
-
-
if (is != null) {
-
try {
-
is.close();
-
} catch (IOException var27) {
-
var27.printStackTrace();
-
}
-
}
-
-
connection.disconnect();
-
}
-
-
return result.toString();
-
}
-
}
3、Java-WebSocket-1.3.9.jar包(上传的是个压缩包,直接解压就能用)
链接: https://pan.百度.com/s/1ypl-bAsmERHn8D2XfdeZBQ?pwd=tcun 提取码: tcun
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgbfgic
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01