WebSocket 1.0的学习和简单使用
生活随笔
收集整理的這篇文章主要介紹了
WebSocket 1.0的学习和简单使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
WebSocket JavaScript API(client)
- websocket.onopen #當打開一個新的連接時會調用這個方法
- websocket.onmessage #當server有數據返回時調用
- websocket.send() #向服務端發送信息,類型包括{String|ArrayBuffer|ArrayBufferView|Blob}
- websocket.close() #向服務器發送關閉的請求,參數{number} [code]??{string} [reason],附相關代碼表,一般使用本方法關閉code為1000,直接關閉瀏覽器為1006,CLOSE_ABNORMAL
| Status code | Name | Description |
| 0-999 | ? | Reserved and not used. |
| 1000 | CLOSE_NORMAL | Normal closure; the connection successfully completed whatever purpose for which it was created. |
| 1001 | CLOSE_GOING_AWAY | The endpoint is going away, either because of a server failure or because the browser is navigating away from the page that opened the connection. |
| 1002 | CLOSE_PROTOCOL_ERROR | The endpoint is terminating the connection due to a protocol error. |
| 1003 | CLOSE_UNSUPPORTED | The connection is being terminated because the endpoint received data of a type it cannot accept (for example, a text-only endpoint received binary data). |
| 1004 | ? | Reserved.?A meaning might be defined in the future. |
| 1005 | CLOSE_NO_STATUS | Reserved.? Indicates that no status code was provided even though one was expected. |
| 1006 | CLOSE_ABNORMAL | Reserved.?Used to indicate that a connection was closed abnormally (that is, with no close frame being sent) when a status code is expected. |
| 1007 | ? | The endpoint is terminating the connection because a message was received that contained inconsistent data (e.g., non-UTF-8 data within a text message). |
| 1008 | ? | The endpoint is terminating the connection because it received a message that violates it's policy. This is a generic status code, used when codes 1003 and 1009 are not suitable. |
| 1009 | CLOSE_TOO_LARGE | The endpoint is terminating the connection because a data frame was received that is too large. |
| 1010 | ? | The client is terminating the connection because it expected the server to negotiate one or more extension, but the server didn't. |
| 1011 | ? | The server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request. |
| 1012-1014 | ? | Reserved for future use by the WebSocket standard. |
| 1015 | ? | Reserved.?Indicates that the connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can't be verified). |
| 1016-1999 | ? | Reserved for future use by the WebSocket standard. |
| 2000-2999 | ? | Reserved for use by WebSocket extensions. |
| 3000-3999 | ? | Available for use by libraries and frameworks.?May not?be used by applications. |
| 4000-4999 | ? | Available for use by applications. |
WebSocket Java API(Server)
@ServerEndpoint("/chatRoomServer") public class MessageEndPoint {private static final ArrayList<Session> sessions;static {sessions = new ArrayList<Session>();}@OnOpenpublic void onOpen(Session session) {sessions.add(session);}@OnMessagepublic void onMessage(String message) {sendMessage(message);}@OnClosepublic void onClose(Session session,CloseReason closeReason) {sessions.remove(session);sendMessage(closeReason.getReasonPhrase());}private void sendMessage(String message){for(Session session : sessions){try {session.getBasicRemote().sendText(message);} catch (IOException e) {e.printStackTrace();}}}}
- @ServerEndpoint("/chatRoomServer"),ServerEndpoint把一個POJO類轉換成了WebSocket EndPoint,后邊的值是訪問地址
- 關于注解請看
| @ServerEndpoint | Declare a Server Endpoint |
| @ClientEndpoint | Declare a Client Endpoint |
| @OnOpen | Declare this method handles open events |
| @OnMessage | Declare this method handles Websocket messages |
| @OnError | Declare this method handles error |
| @OnClose | Declare this method handles WebSocket close events |
轉載于:https://my.oschina.net/ldl123292/blog/300355
總結
以上是生活随笔為你收集整理的WebSocket 1.0的学习和简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 博文视点答题1
- 下一篇: linux下,在挂载设备之前,查看设备的