this is what i have:
- I created android app for my mobile and i'm using app from google play as a server
- I open my hotspot and user can access my wifi with ip and port for example: 192.168.xxx.xxx:8080
He will see my website. There i'm using websockets to pass data between javascript and android java.
In Firefox and Explorer it works fine, but in Chrome it tells me: "Websocket connection to 'ws://192.168.xxx.xxx:9999/' failed: Error during websocket handshake: Status line does not end wit CRLF".
I used the code from here: Writing a WebSocket Server (See file below "EDIT").
Also i read RFC 6455: https://www.rfc-editor.org/rfc/rfc6455 and i do exactly as wrote there.
This example of the ClientSession:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ClientSession {
private Socket socket;
public ClientSession(Socket socket) {
System.out.println("new ClientSessionTest()");
this.socket = socket;
initClientListener();
}
private void initClientListener() {
System.out.println("initClientListener()");
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
BufferedReader socketReader = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
final PrintWriter socketWriter = new PrintWriter(socket
.getOutputStream(), true);
String clientKey = null;
String responseKey = null;
while (true) {
String line = socketReader.readLine();
if (line == null) {
System.out.println("received null from client - closing connection");
break;
} else if (line.isEmpty()) {
System.out.println("empty line");
String _01 = "HTTP/1.1 101 Switching Protocols";
String _02 = "Upgrade: websocket";
String _03 = "Connection: Upgrade";
String _04 = "Sec-WebSocket-Accept: " + responseKey;
String _05 = "Sec-WebSocket-Protocol: chat";
String _06 = "Content-Encoding: identity";
System.out.println(_01);
System.out.println(_02);
System.out.println(_03);
System.out.println(_04);
System.out.println(_05);
System.out.println(_06);
System.out.println("");
socketWriter.println(_01);
socketWriter.println(_02);
socketWriter.println(_03);
socketWriter.println(_04);
socketWriter.println(_05);
socketWriter.println(_06);
socketWriter.println("");
//********************data from client*********************
try {
byte[] buff = new byte[100];
int length = socket.getInputStream().read(buff);
byte[] bstr = new byte[length];
System.arraycopy(buff, 0, bstr, 0, length);
System.out.println(new String(bstr));
for (byte b : bstr) {
System.out.print(((int) b) + " ");
}
System.out.println();
System.out.println();
String str = new String(decodeFrame(buff),"UTF-8");
System.out.println(str);
} catch (Exception e) {
System.out.println(e.getMessage());
//********************************************************
}
} else if (line.startsWith("Sec-WebSocket-Key:")) {
clientKey = line.replace("Sec-WebSocket-Key: ", "");
responseKey = ResponseGenerator
.toResponseKey(clientKey);
} else {
System.out.println("" + line);
//socketWriter.println("lala");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
}
I changed the line String _01 = "HTTP/1.1 101 Switching Protocols"; to String _01 = "HTTP/1.1 101 Switching Protocols\r\n"; and its remove the error (CRLF) i wrote above but the onopen method from the javascript code (below) is not firing and after that also Firefox and Explorer are not working.
Javascript:
<html>
<head>
<script type="text/javascript" >
var websocket;
var url = "ws://localhost:1234";
function init(){
try{
websocket = new MozWebSocket(url, "chat");
}catch(e){
websocket = new WebSocket(url, "char");
}
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
}
var count = 0;
function loop(){
var message = "lala\n";
websocket.send(message);
count++;
setTimeout(loop, 500);
}
function onOpen(event){
alert("Socket has been opened!" + ('5' + 3) + ('5' - 3));
loop();
}
function onMessage(evt){
alert(evt);
}
function onClose(event){
alert("socket closed");
}
function onError(event){
alert(event.data);
}
window.addEventListener("load", init, false);
</script>
</head>
<body>
</body>
</html>
Notice:
I dont have an internet connection (no wifi or 3g) in my mobile. the connection is only from the user to my access point.