开发者

Websocket Java Server. Not sending message nor receiving

I'm trying to make a Java WebSocket server, nothing really complicated, just a little, ugly small server to try the websockets from the browser using the standard defined api.

I've been reading the specification of the protocol and it's quite simple. I've made the handshake and apparently it's working.

What I did with Java is: Opening a socket, receive the request from the new Websocket("ws://...") from the browser's JavaScript, send the server's response creating the "Sec-WebSocket-Accept". All these seems to be working! The google Chrome developers tools show me that the connection was successful... (here is the image)

Websocket Java Server. Not sending message nor receiving

And also I made a ws.onopen = function(ev){ alert("Opened!!"); and that works... (so the connection must be ok right?).

But now, I'm trying to send data from the server to the browser or browser to server, but nothing happens...

To send data from the browser to server I just do a ws.send("datos"); and in the server I try to read this doing a readLine(in) being in = clientSocket.getInputStream();... But nothing... not working...

And to send data from the server to the browser I do a out.write(webSockFrame("data")getBytes()) but again... nothing happens in the browser... in the browser I'm waiting data doing a ws.onmessage = function(ev){....}...

(The webSockFrame(string) function adds the two bytes that the standard requires, one at the beginning and one at the end of the string.)

And I also tried to connect to the server using telnet (to check if the server was working ok), and it worked perfectly, everything, sending and receiving data from the telnet and fro开发者_JS百科m the server...

What am I doing wrong?

I'm using the last version of Chrome (14).

I don't want to build a heavy concurrency server, just a little one to try this new technology.

UPDATE: I don't know what else to try... I did a netstat -a to see if the connection is really active or is just an error of chrome, but for my surprise it exists and is opened until I close the webserver... I don't know why send and ws.onmessage doesn't work...

UPDATE II: I made a change of a text on the event onopen, and the text change... and also made a onclose change of text and when I close the server the text changes... so the connection does exists and the onopen and onclose works perfectly...


You might first want to read the answer to Simple (Java, Javascript) start on Websockets. Your quest is not a new one. There's also some chat on the same topic in the post Easy to Program WebSocket Server. There's a running demo with a link to download all the javascript code (which runs on a server). If you want to follow that blog (last link), there should be a complete tutorial on the javascript side posted there - possibly this week. I got a good start on it today. Also, regularly select About Google Chrome from the wrench to update your chromium browser.


See: Weird websocket behavior: only send data when close the tab

I was just working on a websocket server in C. After writing/sending the handshake header response successfully, I attempt to send some data to the server through the browser or from the server to the browser, but nothing happens! I thought maybe I was doing something wrong and re-read the specifications for websockets but could not resolve the issue! When I saw your post, I decided to test your claim and you're right! Maybe chrome is still fuzzy with websockets. I will try implementing the new frame wrapped messages and see if the client (browser) receives my data. In the mean time, it doesn't look like the browser will flush the data until you close the tab or window... Strange!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜