在WildFly和OpenShift上的WebSocket聊天

聊天是解释WebSocket的最典型示例之一。 它是一个相当常用的界面,可以很容易地解释WebSocket的基本概念。 当然,Java EE 7 WebSocket也有一个, 在这里可用 您可以使用以下步骤在WildFly上轻松运行它:






curl -O http://download.jboss.org/wildfly/8.1.0.Final/wildfly-8.1.0.Final.zip
unzip wildfly-8.1.0.Final.zip
./wildfly-8.1.0.Final/bin/standalone.sh
git clone https://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples
mvn -f websocket/chat/pom.xml wildfly:deploy

然后访问http:// localhost:8080 / chat /

WebSocket的最大优点之一是如何在与HTTP(在本例中为8080)相同的端口上打开套接字。 如果要将此应用程序部署到OpenShift,则可以在端口8000上使用WebSocket进行常规访问,而在端口8443上进行安全访问。 下图对此进行了解释:

在WildFly和OpenShift上的WebSocket聊天

如果要在OpenShift上运行此聊天应用程序,请使用以下步骤:

  1. 单击此处在OpenShift中配置WildFly实例。 将名称更改为“ chatserver”,并将其他所有名称更改为默认值。 单击“创建应用程序”以创建应用程序。
  2. 克隆工作空间:
    git clone ssh://[email protected]/~/git/chatserver.git/
  3. 从以下位置编辑“ javaee7-samples / websocket / chat / src / main / webapp / websocket.js”的第一行:
    var wsUri = "ws://" + document.location.hostname + ":" + document.location.port + document.location.pathname + "chat";

    至:

    var wsUri = "ws://" + document.location.hostname + ":8000" + document.location.pathname + "chat";
  4. 创建WAR文件:
    cd javaee7-samples
    mvn -f websocket/chat/pom.xml
  5. 将生成的WAR文件复制到先前克隆的工作空间中:
    cd ..
    cp javaee7-samples/websocket/chat/target/chat.war chatserver/deployments/ROOT.war
  6. 删除现有文件并将WAR文件添加到git存储库中:
    cd chatserver
    git rm -rf src pom.xml
    git add deployments/ROOT.war
    git commit . -m"updating files"
    git push

    这将输出显示为:

    Counting objects: 6, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (4/4), done.
    Writing objects: 100% (4/4), 6.88 KiB | 0 bytes/s, done.
    Total 4 (delta 1), reused 0 (delta 0)
    remote: Stopping wildfly cart
    remote: Sending SIGTERM to wildfly:285130 ...
    remote: Building git ref 'master', commit 05a7978
    remote: Preparing build for deployment
    remote: Deployment id is 14bcec20
    remote: Activating deployment
    remote: Deploying WildFly
    remote: Starting wildfly cart
    remote: Found 127.2.87.1:8080 listening port
    remote: Found 127.2.87.1:9990 listening port
    remote: /var/lib/openshift/544f08a850044670df00009e/wildfly/standalone/deployments /var/lib/openshift/544f08a850044670df00009e/wildfly
    remote: /var/lib/openshift/544f08a850044670df00009e/wildfly
    remote: CLIENT_MESSAGE: Artifacts deployed: ./ROOT.war
    remote: -------------------------
    remote: Git Post-Receive Result: success
    remote: Activation status: success
    remote: Deployment completed with status: success
    To ssh://[email protected]/~/git/chatserver.git/
    454bba9..05a7978  master -> master

现在,您的聊天服务器位于: http : //chatserver-milestogo.rhcloud.com ,看起来像:

在WildFly和OpenShift上的WebSocket聊天

请享用!

翻译自: https://www.javacodegeeks.com/2014/10/websocket-chat-on-wildfly-and-openshift.html