Jboss EAP 6.4 - Websocket 500 内部服务器错误

Jboss EAP 6.4 - Websocket 500 internal server error

=== Java 控制台日志 ===

JBWEB000236: Servlet.service() for servlet default threw exception: java.lang.IllegalStateException: JBWEB000034: Cannot upgrade from HTTP/1.1 without IO events

=== HTML代码===

<body>
    <div>
        <div id="connect-container">
            <div>
                <fieldset>
                    <legend>Connect or disconnect using WebSocket :</legend>
                    <input type="button" id="connect" onclick="connect();"
                        value="Open Connection" /> <input type="button" id="disconnect"
                        onclick="disconnect();" value="Close Connection" />
                </fieldset>
            </div>
            <div>
                <fieldset>
                    <legend>Type your name below. then click the `Say Hello`
                        button :</legend>
                    <input id="name" type="text" size="40" style="width: 40%" /> <input
                        type="button" id="sayHello" onclick="sendMessage();"
                        value="Say Hello" disabled="disabled" />
                </fieldset>
            </div>
            <div>
                Current WebSocket Connection Status:
                <output id="currentstatus" class="message">Closed</output>
            </div>
            <div>
                <output id="hellomessage" />
            </div>
        </div>
    </div>
</body>

=== Java脚本代码===

var websocket = null;

    function connect() {

        var wsURI = "ws://localhost:8380/websockets-server/helloName";
        websocket = new WebSocket(wsURI);

        websocket.onopen = function() {
            displayStatus('Open');
            document.getElementById('sayHello').disabled = false;
            displayMessage('Connection is now open. Type a name and click Say Hello to send a message.');
        };
        websocket.onmessage = function(event) {
            // log the event                
            displayMessage('The response was received! ' + event.data,
                    'success');
        };
        websocket.onerror = function(event) {
            // log the event
            displayMessage('Error! ' + event.data, 'error');
        };
        websocket.onclose = function() {
            displayStatus('Closed');
            displayMessage('The connection was closed or timed out. Please click the Open Connection button to reconnect.');
            document.getElementById('sayHello').disabled = true;
        };
    }

    function disconnect() {
        if (websocket !== null) {
            websocket.close();
            websocket = null;
        }
        message.setAttribute("class", "message");
        message.value = 'WebSocket closed.';
        // log the event
    }

    function sendMessage() {
        if (websocket !== null) {
            var content = document.getElementById('name').value;
            websocket.send(content);
        } else {
            displayMessage(
                    'WebSocket connection is not established. Please click the Open Connection button.',
                    'error');
        }
    }

    function displayMessage(data, style) {
        var message = document.getElementById('hellomessage');
        message.setAttribute("class", style);
        message.value = data;
    }

    function displayStatus(status) {
        var currentStatus = document.getElementById('currentstatus');
        currentStatus.value = status;
    }

=== Java Class 代码===

package org.jboss.as.quickstarts.websocket_hello;

import javax.websocket.CloseReason;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/helloName")
public class HelloName {

   @OnMessage
   public String sayHello(String name) {
       System.out.println("Say hello to '" + name + "'");
       return ("Hello" + name);
   }

   @OnOpen
   public void helloOnOpen(Session session) {
       System.out.println("WebSocket opened: " + session.getId());
   }

   @OnClose
   public void helloOnClose(CloseReason reason) {
       System.out.println("Closing a WebSocket due to " +   reason.getReasonPhrase());
   }
}

图书馆和XML文件

WEB-INF/lib/jboss-websocket-api_1.0_spec-1.0.0.Final.jar
WEB-INF/jboss-web.xml

jboss-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
   <enable-websockets>true</enable-websockets>
</jboss-web>

浏览器:网络 > WS > Headers

=== 一般 ===

Request URL:ws://localhost:8380/websockets-server/helloName
Request Method:GET
Status Code:500 Internal Server Error

===请求Headers===

Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:th-TH,th;q=0.8,en;q=0.6
Cache-Control:no-cache
Connection:Upgrade
Cookie:JSESSIONID=b+Xfkitt-KYWiRl99vmbubUN
Host:localhost:8380
Origin:http://localhost:8380
Pragma:no-cache
Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
Sec-WebSocket-Key:NH/fCYRMJge2+3WGz9gcLg==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36

=== 响应 Headers ===

Connection:close
Content-Length:1885
Content-Type:text/html;charset=utf-8
Date:Mon, 30 Jan 2017 08:54:00 GMT
Server:Apache-Coyote/1.1

=== 标签框 ===

(Opcode -1)

我相信您忘记配置 HTTP 连接器以使用 NIO。使用websockets需要哪些。

引自JBoss EAP documentation - create a websocket application

Configure the http in the web subsystem of the server configuration file to use the NIO2 protocol.
...
<connector name="http" protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="http" socket-binding="http"/>

谢谢:@SubOptimal

我是这样解决的

此文件:服务器 > XML 配置 > standalone.xml

添加行。

<subsystem xmlns="urn:jboss:domain:web:2.1" default-virtual-server="default-host" native="false">
    <connector name="http" protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="http" socket-binding="http"/>
    <virtual-server name="default-host" enable-welcome-root="true">
        <alias name="websockets-server"/>
    </virtual-server>
</subsystem>