我的html文件中有以下javascript,我试图让websocket在html文件中运行。
<tr>
<td>1</td>
<td>/home/bgautam/Documents/019.dcm.tar.xz</td>
<td><button type="button" class="btn btn-success upload">Upload</button></td>
</tr>
</tbody>
以下脚本文件
$(document).ready(function() {
$(".upload").click(function () {
//geting the name of the file from the url
var fileName=$(this).closest('td').prev().text();
var abcd=fileName.substring(fileName.lastIndexOf("/")+1);
alert(abcd);
//checking whether corresponding file is available in decompressed mode
$.ajax({
url:'/fileExistChecker?location='+fileName,
type : 'POST',
success:function (e) {
if(e=='exist'){
var connection= new WebSocket('ws://localhost:5557');
connection.onopen=function () {
console.log("Connected...");
connection.send(abcd);
console.log("Data sent....")
};
connection.onmessage=function (p1) {
console.log(p1);
};
connection.onerror=function (event) {
connection.send(abcd);
};
connection.onclose=function (p1) {
console.log('connection closed');
};
}
},
error:function (e) {
alert(e);
}
});
});
});
现在我用一个代码
创建了一个java服务器public class ServerCode {
public static void main(String[] args) {
//running server in specific port number
try {
ServerSocket serverSocket = new ServerSocket(5557);
//server is ready for connecting to client
System.out.println("Server is ready to connect to client");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Connected..... 200 OK");
InputStream inputStream = socket.getInputStream();
String data = new Scanner(inputStream, "UTF-8").useDelimiter("\\r\\n\\r\\n").next();
Matcher get = Pattern.compile("^GET").matcher(data);
OutputStream out = socket.getOutputStream();
try {
if (get.find()) {
Matcher match = Pattern.compile("Sec-WebSocket-Key: (.*)").matcher(data);
match.find();
byte[] response = ("HTTP/1.1 101 Switching Protocols\r\n"
+ "Connection: Upgrade\r\n"
+ "Upgrade: websocket\r\n"
+ "Sec-WebSocket-Accept: "
+ DatatypeConverter
.printBase64Binary(
MessageDigest
.getInstance("SHA-1")
.digest((match.group(1) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11")
.getBytes("UTF-8")))
+ "\r\n\r\n")
.getBytes("UTF-8");
out.write(response, 0, response.length);
} else {
out.write("this is sending".getBytes());
}
} catch (Exception e) {
System.out.println(e);
}
System.out.println("i ma here");
//reading message from the client
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
byte[] b;
String clientMessage = null;
/* while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}*/
b=bufferedReader.readLine().getBytes("UTF-8");
System.out.println("i am here");
System.out.println(clientMessage=new String(b));
//sending messsage to client
OutputStream outputStream = socket.getOutputStream();
PrintWriter printWriter = new PrintWriter(outputStream, true);
printWriter.write("Server :" + new Date() + "\n " + clientMessage);
printWriter.close();
bufferedReader.close();
System.out.println("i am here");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
}
System.out.println("i ma hre ");
}
}catch (Exception e){
System.out.println(e);
}
}}
我已经完成了握手,就像你在代码中看到的那样,但大部分是不稳定的,总是给我一些看似加密的输出。我怎样才能做到这一点。