其实使用Http和Https使用的方式是一样的。
建议大家可以看看HttpUtil的源码,感觉设计的挺不错的。
导入Maven依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.1.0</version>
</dependency>
编写测试类(使用Junit单元测试)
@Test
public void testHttps() throws Exception {
JSONObject json = new JSONObject();
json.put("username", "1332788xxxxxx");
json.put("password", "123456.");
String result = HttpRequest.post("https://api.heckjj.com/1/users")
.header("Content-Type", "application/json")
.header("X-heck-Application-Id","2f0419a31f9casdfdsf431f6cd297fdd3e28fds4af")
.header("X-heck-REST-API-Key","1e03efdas82178723afdsafsda4be0f305def6708cc6")
.body(json)
.execute().body();
System.out.println(result);
}
方法解释(上面采用的是一种叫链式编程的方式):
header对应的是请求头。
body对应的是请求体(包含参数和参数值)。
HttpRequest里面包含Post、GET、Delete、Put等常用的RestFul方式。
打印如下:
{"createdAt":"2019-04-30 10:42:07","objectId":"6cfdb77081","sessionToken":"269e433440c9e65b8058d016df703ccb"}
*
* ipSection IP段(以'-'分隔)
* ip 所验证的IP号码
*
*/
public static boolean ipExistsInRange(String ip, String ipSection) {
ipSection = ipSection.trim();
ip = ip.trim();
int idx = ipSection.indexOf('-');
String beginIP = ipSection.substring(0, idx);
String endIP = ipSection.substring(idx + 1);
return getIp2long(beginIP) <= getIp2long(ip) && getIp2long(ip) <= getIp2long(endIP);
}
public static long getIp2long(String ip) {
ip = ip.trim();
String[] ips = ip.split("\\.");
long ip2long = 0L;
for (int i = 0; i < 4; ++i) {
ip2long = ip2long << 8 | Integer.parseInt(ips[i]);
}
return ip2long;
}
public static long getIp2long2(String ip) {
ip = ip.trim();
String[] ips = ip.split("\\.");
long ip1 = Integer.parseInt(ips[0]);
long ip2 = Integer.parseInt(ips[1]);
long ip3 = Integer.parseInt(ips[2]);
long ip4 = Integer.parseInt(ips[3]);
long ip2long = 1L * ip1 * 256 * 256 * 256 + ip2 * 256 * 256 + ip3 * 256 + ip4;
return ip2long;
}
public static boolean isIntranetIP(String ip) {
ipExistsInRange(ip, "172.16.0.0-172.31.255.255") || ip.startsWith("10.");
//10.10.10.116 是否属于固定格式的IP段10.10.1.00-10.10.255.255
String ip="10.10.10.116";
String ipSection="10.10.1.00-10.10.255.255";
boolean exists=ipExistsInRange(ip,ipSection);
System.out.println(exists);
System.out.println(getIp2long(ip));
System.out.println(getIp2long2(ip));
}
使用$JAVA_HOME的话能定位JDK的安装路径的前提是配置了环境变量$JAVA_HOME,否则如下所示,根本定位不到JDK的安装路径
[root@localhost ~]# java -version
java version "1.7.0_65"
OpenJDK Runtime Environment (rhel-2.5.1.2.el6_5-x86_64 u65-b17)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)
[root@localhost ~]# echo $JAVA_HOME
2:which java
首先要申明一下which java是定位不到安装路径的。which java定位到的是java程序的执行路径。那么怎么定位到java的安装路径呢?下面我们来看看例子吧,如下所示:
[root@localhost ~]# java -version
java version "1.7.0_51"
OpenJDK Runtime Environment (rhel-2.4.5.5.el7-x86_64 u51-b31)
OpenJDK 64-Bit Server VM (build 24.51-b03, mixed mode)
[root@localhost ~]# which java
/usr/bin/java
[root@localhost ~]# ls -lrt /usr/bin/java
lrwxrwxrwx. 1 root root 22 9月 6 22:51 /usr/bin/java -> /etc/alternatives/java
[root@localhost ~]# ls -lrt /etc/alternatives/java
lrwxrwxrwx. 1 root root 72 9月 6 22:51 /etc/alternatives/java -> /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.51-2.4.5.5.el7.x86_64/jre/bin/java
意思是无法在根节点 Software\JavaSoft\Prefs 创建命令,实际上就是当前不是管理员权限在运行,需要管理员权限运行。
2.这个错大概的意思就是
警告:无法打开/创建prefs根节点软件\\javasoft \\prefs根节点0x800000002 (这里是百度翻译),但是里面的关键字就是说无法打开,那就代表着权限不够。
3.解决方案
Step1:搜索并运行regedit.exe
Step2: 进入HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft,右击JavaSoft目录,选择新建->项(key),命名为Prefs
Step3: 重新编译即可。
1. SecureRandom 类中 setSeed()底层调用的是 native 方法.所以造成了不同环境之间随机数出现了差别。导致解密不一致问题。
2. 由于linux和window的内核不同造成的!
解决:对加密程序 添加如下两行 代码控制 随机数即可解决问题。然后初始化,就能解决这个问题!
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(PASSWORD.getBytes());
示例如下:
package com.heckjj.utils;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.axis.encoding.Base64;
import org.apache.commons.lang.StringUtils;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class AESUtils {
/**
* 加密
*
* @param content 需要加密的内容
* @param secureKey 加密秘钥
* @return
*/
public static String encrypt(String content, String secureKey) {
try {
if (StringUtils.isEmpty(content)
|| StringUtils.isEmpty(secureKey)) {
return null;
}
KeyGenerator kgen = KeyGenerator.getInstance("AES");
/*
* 问题我已自己解决,这个是由于linux和window的内核不同造成的!
* SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
* secureRandom.setSeed(PASSWORD.getBytes());
* 然后初始化,就能解决这个问题!
*/
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(secureKey.getBytes());
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return encodeBASE64(result); // 加密
} catch (Exception e) {
log.error("加密错误.", e);
}
return null;
}
/**
* 解密
*
* @param content 待解密内容
* @param password secureKey
* @return
*/
public static String decrypt(String content, String secureKey) {
try {
if (StringUtils.isEmpty(content) || StringUtils.isEmpty(secureKey)) {
return null;
}
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(secureKey.getBytes());
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] base64Dec = Base64.decode(content);
byte[] result = cipher.doFinal(base64Dec);
return new String(result);
} catch (Exception e) {
log.warn("解密错误,错误信息是:{}", e);
}
return null;
}
public static String encodeBASE64(byte[] content) throws Exception {
if (content == null || content.length == 0)
return null;
try {
return Base64.encode(content);
} catch (Exception e) {
log.error("Base64 encode error.", e);
return null;
}
}
}
2.去github上面下载winsw: https://github.com/kohsuke/winsw/releases
3. 将WinSW.NET4.exe文件复制到java程序所在文件夹中
4.将java程序重命名,去掉名称中的“.”。例如hccabc-web-1.0.jar ----> hccabc-web.jar
5.将WinSW.exe重命名为hccabc-web.exe(和jar同名)
6. 新建一个xml文件,命名为hccabc-web.xml,写入以下内容(还有一些参数自己去看github说明):
<id>hccabc-web</id>
<name>hccabc-web</name>
<description>鹤城区民政局便民救助平台.</description>
<!-- java环境变量 -->
<env name="JAVA_HOME" value="%JAVA_HOME%"/>
<executable>java</executable>
<arguments>-jar "E:\hccabc\hccabc-web\hccabc-web.war"</arguments>
<!-- 开机启动 -->
<startmode>Automatic</startmode>
<!-- 日志配置 -->
<logpath>%BASE%\logs</logpath>
<logmode>rotate</logmode>
</service>
如果没有配置环境变量,直接将三个文件扔到java的bin目录下运行。去掉标签<env name="JAVA_HOME" value="%JAVA_HOME%"/>
7.命令行定位到当前目录,执行:
hccabc-web.exe install
8. 去windows服务列表中启动程序。
(如果需要更新程序,只需要先将服务停止,再将新文件重命名为hccabc-web.jar,最后启动服务就行了)
昨天我们谈了怎么建立socket通信的服务端和客户端,今天我们就来谈一谈怎么封装报文。
什么是报文这里我就不在阐述了,不清楚的朋友可以自己去查资料。我们今天要谈的报文主要友以下几个部分组成:
3位同步校验位+8位报文长度+报文头+报文体+32位MD5校验位
基本格式如下:
0X110X120X1300000232<?xml version="1.0" encoding="GBK"?><ROOT><Code>0204</Code><Date>20141223</Date><No>141223010008152</No><Code>17010001</Code><Name>张三</Name></ROOT>B251AB76B11114DB176023A0AA27A524
说明:
前面的0X110X120X13是3位16进制的同部位,这里为了大家理解,所以就以字符的形式谢出来了。00000232是报文长度。<?xml version="1.0" encoding="GBK"?><ROOT><Code>0204</Code><Date>20141223</Date><No>141223010008152</No><Code>17010001</Code></ROOT>是报文头。即每个报文都包含的信息。<Name>张三</Name>是报文体。B251AB76B11114DB176023A0AA27A524是加密数据。
关于如何将对象转换为xml格式的报文我将在下一篇写,这里主要是给大家如何将如上的这些字符串转化为字节以及如何发送和接收报文。
1.建立报文的对象
public class SocketPacket {
private String bodyLen;
private String body;
private String syncStr;
private String md5;
public String getBodyLen() {
return bodyLen;
}
public String getBody() {
return body;
}
public String getSyncStr() {
return syncStr;
}
public String getMd5() {
return md5;
}
public void setBodyLen(String bodyLen) {
this.bodyLen = bodyLen;
}
public void setBody(String body) {
this.body = body;
}
public void setSyncStr(String syncStr) {
this.syncStr = syncStr;
}
public void setMd5(String md5) {
this.md5 = md5;
}
public byte[] getByteStream() throws UnsupportedEncodingException{
byte[] bodyBytes = this.body.getBytes("gbk");//获得body的字节数组
int bodyLength = bodyBytes.length;
int socketLength = 3+bodyLength+8+32;
byte [] soc = new byte[socketLength];
//添加校验数据
int index = 0;
soc[0]=0x11;
soc[1]=0x12;
soc[2]=0x13;
index+=3;
//添加8位报文长度(我的博文中也有NumberFormat的用法介绍)
NumberFormat numberFormat = NumberFormat.getNumberInstance();
numberFormat.setMinimumIntegerDigits(8);
numberFormat.setGroupingUsed(false);
byte [] num = numberFormat.format(socketLength).getBytes();
for(int i = 0;i<8;i++){
soc[index++]= num[i];
}
//添加body内容
for(int i = 0;i<bodyLength;i++){
soc[index++] = bodyBytes[i];
}
//添加md5校验码
byte [] md5Bytes = this.md5.getBytes();
for (int i = 0; i < num.length; i++) {
soc[index++] = md5Bytes[i];
}
return soc;
}
//字节装转报文string
public String getString(byte [] socketBytes){
String syncStr = this.bytesToString(socketBytes, 0, 3);
String socketLength = this.bytesToString(socketBytes, 3, 3+8);
String body = this.bytesToString(socketBytes, 3+8, socketBytes.length-32);
String md5 = this.bytesToString(socketBytes,socketBytes.length-32,socketBytes.length);
return syncStr+socketLength+body+md5;
}
//将字节数组转化为string
public String bytesToString(byte [] bytes,int start,int end){
String str = "";
if(bytes.length<end-start){
return str;
}
byte [] bs = new byte[end-start];
for(int i = 0;i<end-start;i++){
bs[i] = bytes[start++];
}
str = new String(bs);
return str;
}
public String toString(){
return this.syncStr+this.bodyLen+this.body+this.md5;
}
}
2.封装发送和接收报文的工具类
/**
* 报文发送
*/
public class SockeUtil {
Socket socket = null;
public SockeUtil(String ip,int port) throws UnknownHostException, IOException{
socket = new Socket(ip, port);
}
//
public SocketPacket sentSocket(SocketPacket socketPacket) throws UnsupportedEncodingException, IOException{
SocketPacket sPacket = new SocketPacket();
OutputStream output=null;
InputStream input =null;
// 同步字符串(3byte)
byte[] sync = null; //
byte[] bodyLen = null; // 8位长度
byte[] body = null; // 内容
byte[] md5 = null; // MD5
output = socket.getOutputStream();
//写数据发送报文
output.write(socketPacket.getByteStream());
//获得服务端返回的数据
input = socket.getInputStream();
sync = this.streamToBytes(input,3);
bodyLen = this.streamToBytes(input, 8);
String lenString = new String(bodyLen);
int len = Integer.valueOf(lenString);
body = this.streamToBytes(input, len);
md5 = this.streamToBytes(input, 32);
sPacket.setSyncStr(new String(sync,Charset.forName("gbk")));
socketPacket.setBodyLen(new String(bodyLen,Charset.forName("gbk")));
socketPacket.setBody(new String(body,Charset.forName("gbk")));
socketPacket.setMd5(new String(md5,Charset.forName("gbk")));
return sPacket;
}
public byte[] streamToBytes(InputStream inputStream,int len){
/**
* inputStream.read(要复制到得字节数组,起始位置下标,要复制的长度)
* 该方法读取后input的下标会自动的后移,下次读取的时候还是从上次读取后移动到的下标开始读取
* 所以每次读取后就不需要在制定起始的下标了
*/
byte [] bytes= new byte[len];
try {
inputStream.read(bytes, 0, len);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bytes;
}
}
3.在封装一个调用报文发送的类:
public String socket(SocketPackage socketPackage) throws UnsupportedEncodingException{
SocketClient socketClient=null;;
try {
socketClient = new SocketClient(ip,端口);
} catch (UnknownHostException e) {
log.error("socket链接异常,链接信息:"+ip+端口);
e.printStackTrace();
} catch (IOException e) {
log.error("socket IO异常");
e.printStackTrace();
}
SocketPackage s = null;
try {
s = socketClient.sendMsg(socketPackage);
} catch (Exception e) {
try {
log.error("socket发送消息异常,发送信息:"+new String(socketPackage.getByteStream(),"GBK"));
} catch (UnsupportedEncodingException e1) {
log.error("socket将socketPackage转为字符串异常,socketPackage信息:"+socketPackage.getByteStream());
e1.printStackTrace();
}
e.printStackTrace();
}
String result = "";
try {
result = new String(s.getStream(),"GBK");
} catch (UnsupportedEncodingException e) {
log.error("socket将socketPackage转为字符串异常,socketPackage信息:"+socketPackage.getByteStream());
e.printStackTrace();
}
return result ;
}
这样我们就能发送报文和接收报文了!赶紧试一下吧!^_^
今天来和大家分享一下java中如何使用socket进行通信。先来啰嗦两句,看看Tcp/ip和udp:
TCP是Transfer Control Protocol的 简称,是一种面向连接的保证可靠传输的协议。通过TCP协议传输,得到的是一个顺序的无差错的数据流。发送方和接收方的成对的两个socket之间必须建 立连接,以便在TCP协议的基础上进行通信,当一个socket(通常都是server socket)等待建立连接时,另一个socket可以要求进行连接,一旦这两个socket连接起来,它们就可以进行双向数据传输,双方都可以进行发送 或接收操作。
UDP是User Datagram Protocol的简称,是一种无连接的协议,每个数据报都是一个独立的信息,包括完整的源地址或目的地址,它在网络上以任何可能的路径传往目的地,因此能否到达目的地,到达目的地的时间以及内容的正确性都是不能被保证的。
(一)两者之间的比较
UDP:
- 每个数据报中都给出了完整的地址信息,因此无需要建立发送方和接收方的连接。
- UDP传输数据时有大小限制的,每个被传输的数据报必须限定在64KB之内。
- UDP是一个不可靠的协议,发送方所发送的数据报并不一定以相同的次序到达接收方。
TCP:
- 面向连接的协议,在socket之间进行数据传输之前必然要建立连接,所以在TCP中需要连接时间。
- TCP传输数据大小限制,一旦连接建立起来,双方的socket就可以按统一的格式传输大的 数据。
- TCP是一个可靠的协议,它确保接收方完全正确地获取发送方所发送的全部数据。
(二)应用
- TCP在网络通信上有极强的生命力,例如远程连接(Telnet)和文件传输(FTP)都需要不定长度的数据被可靠地传输。但是可靠的传输是要付出代价的,对数据内容正确性的检验必然占用计算机的处理时间和网络的带宽,因此TCP传输的效率不如UDP高。
- UDP操作简单,而且仅需要较少的监护,因此通常用于局域网高可靠性的分散系统中client/server应用程序。例如视频会议系统,并不要求音频视频数据绝对的正确,只要保证连贯性就可以了,这种情况下显然使用UDP会更合理一些。
注:以上内容是在网上找的,为了节省时间,我就不再自己写了。
下面我们来看看如何搭建socket环境:
socket通信分为客户端和服务器端。服务器端会不停的监听,当服务器端监听到有客户端向其发送通信请求的时候,双方建立连接。通信完毕后,双方关闭连接。
首先我们来看如何搭建客户端:
public class SocketClient {
public static void main(String[] args) throws IOException{
try{
Socket socket=new Socket("127.0.0.1",5200);
System.out.println("client start ...");
//向本机的52000端口发出客户请求
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//由系统标准输入设备构造BufferedReader对象
PrintWriter write=new PrintWriter(socket.getOutputStream());
//由Socket对象得到输出流,并构造PrintWriter对象
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//由Socket对象得到输入流,并构造相应的BufferedReader对象
String readline;
readline=br.readLine(); //从系统标准输入读入一字符串
while(!readline.equals("end")){
//若从标准输入读入的字符串为 "end"则停止循环
write.println(readline);
//将从系统标准输入读入的字符串输出到Server
write.flush();
//刷新输出流,使Server马上收到该字符串
System.out.println("Client:"+readline);
//在系统标准输出上打印读入的字符串
System.out.println("Server:"+in.readLine());
//从Server读入一字符串,并打印到标准输出上
readline=br.readLine(); //从系统标准输入读入一字符串
} //继续循环
write.close(); //关闭Socket输出流
in.close(); //关闭Socket输入流
socket.close(); //关闭Socket
}catch(Exception e) {
System.out.println("can not listen to:"+e);//出错,打印出错信息
}
}
}
下面是服务器端得搭建:
public class SocketService {
public static void main(String[] args) throws IOException{
SocketService socketService = new SocketService();
socketService.oneServer();
}
public void oneServer(){
try{
ServerSocket server=null;
try{
server=new ServerSocket(5200);
System.out.println("server start is ok...");
//创建一个ServerSocket在端口5200监听客户请求
}catch(Exception e) {
System.out.println("can not listen to:"+e);
//出错,打印出错信息
}
Socket socket=null;
try{
socket=server.accept();
//使用accept()阻塞等待客户请求,有客户
//请求到来则产生一个Socket对象,并继续执行
}catch(Exception e) {
System.out.println("Error."+e);
//出错,打印出错信息
}
String line;
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//由Socket对象得到输入流,并构造相应的BufferedReader对象
PrintWriter writer=new PrintWriter(socket.getOutputStream());
//由Socket对象得到输出流,并构造PrintWriter对象
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//由系统标准输入设备构造BufferedReader对象
System.out.println("Client:"+in.readLine());
//在标准输出上打印从客户端读入的字符串
line=br.readLine();
//从标准输入读入一字符串
while(!line.equals("end")){
//如果该字符串为 "bye",则停止循环
writer.println(line);
//向客户端输出该字符串
writer.flush();
//刷新输出流,使Client马上收到该字符串
System.out.println("Server:"+line);
//在系统标准输出上打印读入的字符串
System.out.println("Client:"+in.readLine());
//从Client读入一字符串,并打印到标准输出上
line=br.readLine();
//从系统标准输入读入一字符串
} //继续循环
writer.close(); //关闭Socket输出流
in.close(); //关闭Socket输入流
socket.close(); //关闭Socket
server.close(); //关闭ServerSocket
}catch(Exception e) {//出错,打印出错信息
System.out.println("Error."+e);
}
}
}
这是我们先启动服务器端,再启动客户端(顺序不能乱),当我在客户端输入abc时,如下:
我们再打开服务器端得控制台,会看到客户端发送的消息:
然后我们再输入123:
我们再打开客户端得控制台:
这里显示了服务端回传的信息,证明我们的通信是没有问题的了。
以上的服务端只能监听一个客户端,要想是想监听多个客户端,我们对服务端做一下修改:
public void manyServer() throws IOException{
boolean flag = true;
ServerSocket serverSocket = null;
serverSocket = new ServerSocket(5200);
int clientNum = 0;
while(flag){
new SocketServerTherd(serverSocket.accept(), clientNum).start();
clientNum++;
}
serverSocket.close();
}
public class SocketServerTherd extends Thread{
Socket socket = null;
int clientNum = 0;
public SocketServerTherd(Socket socket,int num){
this.socket = socket;
this.clientNum = num+1;
}
public void run(){
try{
String line;
BufferedReader in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//由Socket对象得到输入流,并构造相应的BufferedReader对象
PrintWriter writer=new PrintWriter(socket.getOutputStream());
//由Socket对象得到输出流,并构造PrintWriter对象
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//由系统标准输入设备构造BufferedReader对象
System.out.println("Client:"+in.readLine());
//在标准输出上打印从客户端读入的字符串
line=br.readLine();
//从标准输入读入一字符串
while(!line.equals("end")){
//如果该字符串为 "bye",则停止循环
writer.println(line);
//向客户端输出该字符串
writer.flush();
//刷新输出流,使Client马上收到该字符串
System.out.println("Server:"+line);
//在系统标准输出上打印读入的字符串
System.out.println("Client:"+in.readLine());
//从Client读入一字符串,并打印到标准输出上
line=br.readLine();
//从系统标准输入读入一字符串
} //继续循环
writer.close(); //关闭Socket输出流
in.close(); //关闭Socket输入流
socket.close(); //关闭Socket
}catch(Exception e) {//出错,打印出错信息
System.out.println("Error."+e);
}
}
}
今天就先说到这里吧,明天我将告诉大家如何利用报文进行通行。
heartCheckFun(){
var that = this;
//心跳检测,每20s心跳一次
that.heartCheck = {
timeout: 20000,
timeoutObj: null,
serverTimeoutObj: null,
reset: function(){
clearTimeout(this.timeoutObj);
clearTimeout(this.serverTimeoutObj);
return this;
},
start: function(){
var self = this;
this.timeoutObj = setTimeout(function(){
//这里发送一个心跳,后端收到后,返回一个心跳消息,
//onmessage拿到返回的心跳就说明连接正常
that.websocketSend("HeartBeat");
console.info("客户端发送心跳");
self.serverTimeoutObj = setTimeout(function(){//如果超过一定时间还没重置,说明后端主动断开了
that.websock.close();//如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
}, self.timeout)
}, this.timeout)
}
}
},
nginx的设置为:
location /{
proxy_pass http://xx.xxx.xxx.xx:xxxx;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 2000s;
keepalive_timeout 2000s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
尽管设置2000s,但是当到达设置时间,如果websocket没有进行通讯,还是会断开连接.可以前端做一个心跳检测,每隔20秒钟发起一次通信就行.
希望能帮到你;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://localhost:6633/;
}