简体   繁体   English

Websocket Java Server。 不发送消息也不接收

[英]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. 我正在尝试创建一个Java WebSocket服务器,没有什么真正复杂的,只是一个小丑陋的小服务器,使用标准定义的api从浏览器尝试websockets。

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". 我用Java做的是:打开一个套接字,从浏览器的JavaScript接收来自new Websocket("ws://...")的请求,发送服务器的响应,创建“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) 谷歌Chrome开发人员工具向我显示连接成功...(这是图像)

连接

And also I made a ws.onopen = function(ev){ alert("Opened!!"); 而且我做了一个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"); 要将数据从浏览器发送到服务器,我只需要执行ws.send("datos"); and in the server I try to read this doing a readLine(in) being in = clientSocket.getInputStream(); 在服务器中我尝试读取这个做readLine(in) 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){....} ... 并且要将数据从服务器发送到浏览器,我执行out.write(webSockFrame("data")getBytes())但是再次......浏览器中没有任何反应...在浏览器中我正在等待数据执行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.) webSockFrame(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 from the server... 我还尝试使用telnet连接到服务器(检查服务器是否正常工作),它完美地工作,从telnet和服务器发送和接收数据...

What am I doing wrong? 我究竟做错了什么?

I'm using the last version of Chrome (14). 我正在使用最新版本的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... 更新:我不知道还有什么可以尝试...我做了一个netstat -a来查看连接是否真的是活动的,或者只是一个chrome的错误,但我惊讶它存在并打开直到我关闭webserver ...我不知道为什么发送和ws.onmessage不起作用...

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... 更新II:我更改了onopen事件上的文本,并更改​​了文本...并且还对文本进行了近距离更改,当我关闭服务器时,文本发生了更改...因此连接确实存在且onopen和onclose工作完美......

See: Weird websocket behavior: only send data when close the tab 请参阅: 奇怪的websocket行为:仅在关闭选项卡时发送数据

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! 我刚刚在C中使用websocket服务器。成功写入/发送握手头响应后,我尝试通过浏览器或从服务器向浏览器发送一些数据,但没有任何反应! I thought maybe I was doing something wrong and re-read the specifications for websockets but could not resolve the issue! 我想也许我做错了什么并重新阅读websockets的规格但无法解决问题! When I saw your post, I decided to test your claim and you're right! 当我看到你的帖子时,我决定测试你的说法,你是对的! Maybe chrome is still fuzzy with websockets. 也许chrome还是模糊的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! 同时,在关闭选项卡或窗口之前,浏览器看起来不会刷新数据...奇怪!

You might first want to read the answer to Simple (Java, Javascript) start on Websockets . 您可能首先想要在Websockets上阅读Simple(Java,Javascript)start的答案。 Your quest is not a new one. 你的任务不是新任务。 There's also some chat on the same topic in the post Easy to Program WebSocket Server . 易于编程WebSocket服务器的帖子中也有一些关于同一主题的聊天。 There's a running demo with a link to download all the javascript code (which runs on a server). 有一个正在运行的演示链接下载所有的JavaScript代码(运行在服务器上)。 If you want to follow that blog (last link), there should be a complete tutorial on the javascript side posted there - possibly this week. 如果你想关注那个博客(最后一个链接),那么应该有一个关于javascript方面的完整教程 - 可能是本周。 I got a good start on it today. 我今天开了个好头。 Also, regularly select About Google Chrome from the wrench to update your chromium browser. 此外,定期从扳手中选择“关于Google Chrome”以更新您的Chrome浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft Azure IoT中心(java)通知“与服务器的连接丢失。 发送消息后,重新连接0次。” - Microsoft Azure IoT hub (java) notifies a “Lost connection to the server. Reconnecting 0 time.” after sending a message Java EE Websocket:如何在不接收消息的情况下继续发送数据? - Java EE Websocket: How to send keep sending data without receiving message? Java客户端到C#服务器。 没有收到负数 - Java Client to C# server. Not receiving negative numbers Java Websocket客户端未收到服务器的响应 - Java Websocket Client Not Receiving Response from Server 将填充的对象发送到服务器并接收回消息 - Sending populated object to server and receiving message back 套接字未从服务器接收消息或向服务器发送消息 - Socket not receiving or sending message from/to server 收发Java Midi Sysex消息 - Sending and receiving Java Midi Sysex message 在Java服务器中发送EditText时出错。 看到代码 - Error sending edittext in java server. See code 从Java websocket服务器发送图像 - Sending an image from a Java websocket server 在Java中收到websocket消息后取消TimerTask运行的文件监视程序 - Canceling filewatcher run by TimerTask after receiving websocket message in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM