Websocket Java 服务器.不发送消息也不接收

编程入门 行业动态 更新时间:2024-10-25 10:27:15
本文介绍了Websocket Java 服务器.不发送消息也不接收的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试制作一个 Java WebSocket 服务器,没有什么特别复杂的,只是一个很小的、丑陋的小型服务器,可以使用标准定义的 api 从浏览器中尝试 websocket.

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.

我用 Java 所做的是:打开一个套接字,从浏览器的 JavaScript 接收来自 new Websocket("ws://...") 的请求,发送服务器的响应,创建Sec-WebSocket-接受".所有这些似乎都在起作用!谷歌浏览器开发者工具显示连接成功...(这是图片)

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)

而且我还做了一个 ws.onopen = function(ev){ alert("Opened!!"); 并且它有效......(所以连接一定没问题吧?).

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...

要将数据从浏览器发送到服务器,我只需执行一个 ws.send("datos"); 并在服务器中我尝试通过 readLine(in) 正在 in = clientSocket.getInputStream();... 但没有什么...不起作用...

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...

为了将数据从服务器发送到浏览器,我执行了一个 out.write(webSockFrame("data")getBytes()) 但同样......浏览器中没有任何反应......在浏览器中,我正在等待数据执行 ws.onmessage = function(ev){....}...

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){....}...

(webSockFrame(string) 函数添加了标准要求的两个字节,一个在字符串的开头,一个在字符串的结尾.)

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

而且我还尝试使用 telnet 连接到服务器(以检查服务器是否正常工作),并且一切正常,从 telnet 和服务器发送和接收数据...

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...

我做错了什么?

我使用的是最新版本的 Chrome (14).

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.

更新:我不知道还有什么可尝试的...我做了一个 netstat -a 来查看连接是否真的处于活动状态或者只是 chrome 的错误,但令我惊讶的是它存在并且一直打开直到我关闭网络服务器...我不知道为什么 send 和 ws.onmessage 不起作用...

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...

更新 II:我对 onopen 事件的文本进行了更改,并且文本发生了变化……并且还对文本进行了 onclose 更改,当我关闭服务器时,文本发生了变化……因此连接确实存在并且 onopen 和 onclose 完美地工作......

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...

推荐答案

参见:奇怪的 websocket 行为:仅在关闭选项卡时发送数据

我刚刚在 C 语言的 websocket 服务器上工作.在成功写入/发送握手标头响应后,我尝试通过浏览器或从服务器到浏览器向服务器发送一些数据,但没有任何反应!我想也许我做错了什么并重新阅读了 websockets 的规范,但无法解决问题!当我看到你的帖子时,我决定测试你的主张,你是对的!也许 chrome 对 websockets 仍然模糊.我将尝试实现新的帧包装消息,并查看客户端(浏览器)是否收到我的数据.同时,在您关闭选项卡或窗口之前,浏览器似乎不会刷新数据......奇怪!

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!

这篇关于Websocket Java 服务器.不发送消息也不接收的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 05:21:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1129031.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:也不   发送消息   服务器   Websocket   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!