-1

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

=== Javascript 代码 ===

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 类代码 ===

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 > 标题

=== 一般 ===

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

=== 请求标头 ===

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

=== 响应头 ===

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)
4

2 回答 2

2

我相信您忘记将 HTTP 连接器配置为使用 NIO。这是使用 websockets 所需要的。

引用JBoss EAP 文档 - 创建一个 websocket 应用程序

在服务器配置文件的web子系统中配置http使用NIO2协议。
...
<connector name="http" protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="http" socket-binding="http"/>

于 2017-01-30T10:14:26.313 回答
0

谢谢:@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>
于 2017-01-30T17:09:38.943 回答