websocket 调用接口示例:
public static String sendSj(String ip, int port, String encoding, String data) {
StringBuilder stb = new StringBuilder();
boolean isAllSuccess = true;
try (Socket clientSocket = new Socket(ip, port);
DataInputStream dataIS = new DataInputStream(clientSocket.getInputStream());
InputStreamReader inSR = new InputStreamReader(dataIS, encoding);
BufferedReader br = new BufferedReader(inSR);
DataOutputStream dataOS = new DataOutputStream(clientSocket.getOutputStream());
OutputStreamWriter outSW = new OutputStreamWriter(dataOS, encoding);
BufferedWriter bw = new BufferedWriter(outSW);) {
// 发送数据
bw.write(data);
bw.flush();
String info = "";
// 接收数据
while ((info = br.readLine()) != null) {
stb.append(info);
}
} catch (UnknownHostException e) {
log.error("创建socket客户端出错" + e.getMessage(), e);
isAllSuccess = false;
} catch (IOException e) {
log.error("socket客户端获取流出错" + e.getMessage(), e);
isAllSuccess = false;
}
if (!isAllSuccess) {
MessageUtils.addErrorMsg("调用接口失败,请联系管理员处理");
throw new CallJhjkException("调用接口失败,请联系管理员处理");
}
return stb.toString();
}