gpt4 book ai didi

grails - Grails javax.websocket问题

转载 作者:行者123 更新时间:2023-12-02 14:43:02 25 4
gpt4 key购买 nike

Grails 2.3.7-Java 1.7

我已经看到以下示例在核心Java中使用并作为演示,试图在Grails中实现相同的效果,我知道websockets周围有一些插件,但是我试图自己解决这个问题:

Controller 1

package chat

class TestController {

def index() { }
}

index.gsp
<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="main">
<g:set var="entityName" value="${message(code: 'admin.label', default: 'Admin')}" />
<title><g:message code="default.create.label" args="[entityName,BAH,BAH]" /></title>
</head>
<body>
<form>
<input id="textMessage" type="text">
<input type="button" value="send" onClick="sendMessage();">
</form>
<br>
<textarea id="messagesTextarea" rows="10" cols="50">
</textarea>

<script type="text/javascript">
var webSocket=new WebSocket("ws://localhost:8080/chat/testing");
var messagesTextarea=document.getElementById("messagesTextarea");
var textMessage=document.getElementById("textMessage");
webSocket.onopen=function(message) {processOpen(message);};
webSocket.onmessage=function(message) {processMessage(message);};
webSocket.onclose=function(message) {processClose(message);};
webSocket.onerror=function(message) {processError(message);};
function processOpen(message) {
messagesTextarea.value +=" Server Connect.... "+"\n";
}
function processMessage(message) {
messagesTextarea.value +=" Receive from Server ===> "+ message.data +"\n";
}
function sendMessage() {
if (textMssage.value!="close") {
webSocket.send(textMessage.value);
messagesTextarea.value +=" Send to Server ===> "+ textMessage.value +"\n";
textMessage.value="";
}else {
websocket.close();
}
}
function processClose(message) {
webSocket.send("Client disconnected......");
messagesTextarea.value +="Server Disconnected... "+"\n";
}
function processError(message) {
messagesTextarea.value +=" Error.... \n";
}
</script>
</body>
</html>

Controller 2:
package chat

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/testing")
class TestingController {

@OnOpen
public void handleOpen() {
System.out.println("Client is now connected.");
}

@OnMessage
public String handleMessage(String message) {

System.out.println("Client sent: " + message);
String replyMessage = "echo "+message;
System.out.println("Send to Client: " + replyMessage);
return replyMessage;
}

@OnClose
public void handeClose() {
System.out.println("Client is now disconnected.");
}

@OnError
public void handleError(Throwable t) {
t.printStackTrace();
}

}

当我运行应用程序时就这样

我在chrome中收到以下错误:
WebSocket connection to 'ws://localhost:8080/chat/testing' failed: Error during WebSocket handshake: Unexpected response code: 404 index:37
WebSocket is already in CLOSING or CLOSED state.

和在textArea中
Error.... 
Server Disconnected...

在ggts控制台上,我看到:
Client sent: null
Send to Client: echo null

最初,我以这种方式尝试了 Controller :
package chat


class TestingController extends TestingEndpoint {

}

并在src / java / chat中
package chat;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.server.ServerEndpoint;


@ServerEndpoint("/testing")
class TestingEndpoint {

@OnOpen
public void handleOpen() {
System.out.println("Client is now connected.");
}
@OnMessage
public String handleMessage(String message) {

System.out.println("Client sent: " + message);
String replyMessage = "echo "+message;
System.out.println("Send to Client: " + replyMessage);
return replyMessage;
}
@OnClose
public void handeClose() {
System.out.println("Client is now disconnected.");
}
@OnError
public void handleError(Throwable t) {
t.printStackTrace();
}

}

除了ggts控制台中没有任何内容外,此方法产生了相同的结果

想知道是否有人可以在Grails中使用javax.websocket。

最佳答案

好的就可以了-毕竟还不错

解决方法是:

  • 在gsp中有一些错别字:

  • index.gsp
    <!DOCTYPE html>
    <html>
    <head>
    <meta name="layout" content="main">
    <g:set var="entityName" value="${message(code: 'admin.label', default: 'Admin')}" />
    <title><g:message code="default.create.label" args="[entityName,BAH,BAH]" /></title>
    </head>
    <body>
    <form>
    <input id="textMessage" type="text">
    <input type="button" value="send" onClick="sendMessage();">
    </form>
    <br>
    <textarea id="messagesTextarea" rows="10" cols="50">
    </textarea>

    <script type="text/javascript">
    var webSocket=new WebSocket("ws://localhost:8080/chat/annotated");
    var messagesTextarea=document.getElementById("messagesTextarea");
    webSocket.onopen=function(message) {processOpen(message);};
    webSocket.onmessage=function(message) {processMessage(message);};
    webSocket.onclose=function(message) {processClose(message);};
    webSocket.onerror=function(message) {processError(message);};
    function processOpen(message) {
    messagesTextarea.value +=" Server Connect.... "+"\n";
    }
    function processMessage(message) {
    messagesTextarea.value +=" Receive from Server ===> "+ message.data +"\n";
    }
    function sendMessage() {

    if (textMessage.value!="close") {
    webSocket.send(textMessage.value);
    messagesTextarea.value +=" Send to Server ===> "+ textMessage.value +"\n";
    textMessage.value="";
    }else {
    websocket.close();
    }
    }
    function processClose(message) {
    webSocket.send("Client disconnected......");
    messagesTextarea.value +="Server Disconnected... "+"\n";
    }
    function processError(message) {
    messagesTextarea.value +=" Error.... \n";
    }
    </script>
    </body>
    </html>

    现在是终点的实际修复方法,我从这里偶然发现了它:
    https://tyrus.java.net/documentation/1.7/index/deployment.html
    示例3.2使用ServerContainer部署带注释的端点

    所以解决方法是添加src / java / MyServletContextListenerAnnotated.java
    package chat;

    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    import javax.websocket.DeploymentException;
    import javax.websocket.OnClose;
    import javax.websocket.OnError;
    import javax.websocket.OnMessage;
    import javax.websocket.OnOpen;
    import javax.websocket.server.ServerContainer;
    import javax.websocket.server.ServerEndpoint;



    @WebListener
    @ServerEndpoint("/annotated")
    public class MyServletContextListenerAnnotated implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
    final ServerContainer serverContainer = (ServerContainer) servletContextEvent.getServletContext()
    .getAttribute("javax.websocket.server.ServerContainer");

    try {
    serverContainer.addEndpoint(MyServletContextListenerAnnotated.class);
    } catch (DeploymentException e) {
    e.printStackTrace();
    }
    }

    /* @OnMessage
    public String onMessage(String message) {
    return message;
    }
    */
    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
    @OnOpen
    public void handleOpen() {
    System.out.println("Client is now connected.");
    }
    @OnMessage
    public String handleMessage(String message) {

    System.out.println("Client sent: " + message);
    String replyMessage = "echo "+message;
    System.out.println("Send to Client: " + replyMessage);
    return replyMessage;
    }
    @OnClose
    public void handeClose() {
    System.out.println("Client is now disconnected.");
    }
    @OnError
    public void handleError(Throwable t) {
    t.printStackTrace();
    }
    }

    由于gsp中的端点已更新为使用新端点,因此最后的任务是将_Events.groovy添加到脚本中:
    import groovy.xml.StreamingMarkupBuilder

    eventWebXmlEnd = {String tmpfile ->
    def root = new XmlSlurper().parse(webXmlFile)
    root.appendNode {
    'listener' {
    'listener-class' (
    'chat.MyServletContextListenerAnnotated'
    )
    }
    }

    webXmlFile.text = new StreamingMarkupBuilder().bind {
    mkp.declareNamespace(
    "": "http://java.sun.com/xml/ns/javaee")
    mkp.yield(root)
    }
    }

    并在这里-客户端连接服务器发送等等

    关于grails - Grails javax.websocket问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24889317/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com