WebSocket教程02(簡單的聊天室Web應用。-Java ServerEndpoint + JavaScript客戶端)



00:06創建以GlassFish 4作為目標運行時的WebSocketPrj02 Web項目
00:25創建一個websocket ServerEndpoint和一個html客戶端
01:07代碼ChatroomServerEndpoint
01:28跟蹤聊天室用戶集
02:05建立新的連接功能
02:52關閉連接功能
03:20接收消息功能
06:13創建並填充JSON消息
07:28代碼default.html客戶端
08:54處理來自伺服器的傳入JSON消息
10:01發送JSON消息功能
10:39試運行完成的應用程序

網站+下載源代碼@ https://zaneacademyapp.appspot.com或http://zaneacademy.com或直接@https://sites.fastspring.com/zaneacademy/product/all下載。

12 comments
  1. WebSocket Security Tutorial – Setup and run on both GlassFish 4.1 & Tomcat 8 @ https://youtu.be/Am5eTQ-sQn4
    WebSocket Tutorial 15 – ServerEndpoint + CDI + Stateless, Stateful, and Singleton Session Beans @ https://youtu.be/McifXIboiRk
    WebSocket Tutorial 14 – WebSocket Path Mapping + Java Servlet + HttpSession + glassFish 4 + tomcat 8 @ https://youtu.be/92fM1JPqDVI
    WebSocket Tutorial 13 – ServerEndpoint + Java Servlet + HttpSession + glassFish 4 + tomcat 8 @ https://youtu.be/YE8my5AF67s
    WebSocket Java Swing Sketch App. Tutorial (Tomcat 8 + Java 8) @ https://youtu.be/BzQ9KUbfLek
    WebSocket JavaScript Sketch App. Tutorial @ https://youtu.be/wU-FRsLky3Q
    WebSocket JavaFX Sketch App. Tutorial (Tomcat 8 + Java 8) @ https://youtu.be/2xgnSpcAVzE
    WebSocket Tutorial 09 (ClientEndpoint JavaFX chat app w/ encoders & decoders) @ https://youtu.be/0nTqlVOI2Rc
    WebSocket Tutorial 08 (JavaFX Programmatic ClientEndpoint Chat App.) @ https://youtu.be/EVL7fUTrFl4
    WebSocket Tutorial 07 – ClientEndpoint Java Application @ https://youtu.be/KVPXQWJagDg
    WebSocket Tutorial 06 (roundtrip JSON + Message handling) @ https://youtu.be/KmSpk8XWeQY
    WebSocket Tutorial 05 (Programmatic Endpoint chatroom web app.) @ https://youtu.be/nLe8vuxW8b4
    WebSocket Tutorial 04 (Enhanced chatroom web application) @ https://youtu.be/NjUbKcEcLQA
    WebSocket Tutorial 03 (encoders & decoders in a simple chatroom web app.) @ https://youtu.be/4nWZ2Oog_w8
    WebSocket Tutorial 02 (Simple chatroom web app. – Java ServerEndpoint + JavaScript Client) @ https://youtu.be/BikL52HYaZg
    WebSocket (Tutorial 01 – Java Server + JavaScript Client + GlassFish 4.0 + JDK 1.7) @ https://youtu.be/_Fi4vz6oUio

  2. Hello!I have this problem.When I use GlassFish it works fine but when I use Tomcat (8.5) endpoint closes when I press button on my web page so in text area I can see starting message when endpoint was opened and message when it closes and nothing else. So every time I type my username in text field as a first message endpoints instantly closes! Java 8+Tomcat 8.5

  3. I keep getting this "Firefox cant establish a connection to the server at ws://localhost:8080/WebSocketPrj02/chatroomServerEndpoint" error on console while running it from Netbeans 8.02, java 8 with glassfish. When trying to send a message, it shows "invalid state error". Also shows, "The connection to ws://….. was interrupted while the page was loading". In Chrome it shows nothing while loading, but when sending message it shows the same invalid state error as on Firefox. Please help…..

  4. Hi,
     After downloaded, I have ran using Tomcat8 and getting below error

    [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/WebSocketChatApp]]
     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
     … 6 more
    Caused by: java.lang.NoClassDefFoundError: javax/json/JsonStructure
     at java.lang.Class.getDeclaredMethods0(Native Method)
     at java.lang.Class.privateGetDeclaredMethods(Class.java:2570)
     at java.lang.Class.getDeclaredMethods(Class.java:1855)
     at org.apache.tomcat.websocket.pojo.PojoMethodMapping.<init>(PojoMethodMapping.java:86)
     at org.apache.tomcat.websocket.server.WsServerContainer.addEndpoint(WsServerContainer.java:245)
     at org.apache.tomcat.websocket.server.WsSci.onStartup(WsSci.java:120)
     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5156)
     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

Comments are closed.