[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-20。"],[[["\u003cp\u003eWebSockets offer a full-duplex communication channel between clients and servers, enabling real-time data exchange and reducing latency.\u003c/p\u003e\n"],["\u003cp\u003eTypical use cases for WebSockets include real-time event updates, user notifications, chatting applications, collaborative editing tools, and multiplayer games.\u003c/p\u003e\n"],["\u003cp\u003eSession affinity, which routes requests from the same user to the same instance, can be enabled in App Engine to accommodate HTTP long polling as a fallback for clients not supporting WebSockets.\u003c/p\u003e\n"],["\u003cp\u003eSession affinity in App Engine is not guaranteed and should be used only for allowing fall back from libraries such as socket.io, and not for stateful applications.\u003c/p\u003e\n"],["\u003cp\u003eSession affinity can be enabled or disabled at the version level in App Engine by modifying the \u003ccode\u003eapp.yaml\u003c/code\u003e file, ensuring that clients have cookies enabled for it to function.\u003c/p\u003e\n"]]],[],null,["# Creating persistent connections with WebSockets\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nGo Java Node.js PHP Python Ruby .NET Custom\n\nYou can use WebSockets to create a persistent connection from a client (such\nas a mobile device or a computer) to an App Engine instance. The open\nconnection allows two-way data exchange between the client and the server at any\ntime, resulting in lower latency and better use of resources.\n\nWebSockets\n----------\n\nThe WebSockets protocol, defined in [RFC 6455](https://tools.ietf.org/html/rfc6455),\nprovides a full-duplex communication channel between a client and a server. The\nchannel is initiated from an HTTP(S) request with an \"upgrade\" header.\n\nTypical use cases for WebSockets include:\n\n- Real time event updates, such as social media feeds, sports scores, news, or stock market prices\n- User notifications, such as software or content updates\n- Chatting applications\n- Collaborative editing tools\n- Multiplayer games\n\nWebSockets are always available to your application without any additional setup.\nOnce a WebSockets connection is established, it will time out after one hour.\nUsage of the WebSocket is [billed by connection usage](/appengine/pricing#managing)\nuntil timeout or socket termination.\n\n\u003cbr /\u003e\n\nRunning a sample application with WebSockets\n--------------------------------------------\n\nThe code samples in this document describe how to run a sample application with\nWebsockets.\n\n\n### Prerequisites and setup\n\nFollow the instructions in\n[Setting Up Your Development Environment](/appengine/docs/flexible/setting-up-environment)\nto set up your environment and project, and to understand how apps are structured.\n\n### Clone the sample app\n\nCopy the sample apps to your local machine, and navigate to the `websockets`\ndirectory:\n\n\u003cbr /\u003e\n\n### Run the sample locally\n\n\u003cbr /\u003e\n\n### Deploy and run the sample on App Engine\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nSession affinity\n----------------\n\nNot all clients support WebSockets. To work around this, many applications\nuse libraries such as [socket.io](http://socket.io/) that fall back on http long\npolling with clients that don't support WebSockets.\n\nApp Engine typically distributes requests evenly among available instances.\nHowever, when using http long polling, multiple sequential requests from a given\nuser need to reach the same instance.\n\nTo allow App Engine to send requests by the same user to the same instance,\nyou can enable session affinity. App Engine then identifies which requests\nare sent by the same users by inspecting a cookie and routes those requests to\nthe same instance.\n| **Important:** App Engine applications must always be tolerant of session affinity interruptions, particularly because all App Engine instances are periodically restarted. Enabling session affinity can also limit the effectiveness of App Engine's load balancing algorithms and can cause your instance to become overloaded.\n\nSession affinity in App Engine is implemented on a best-effort basis. When\ndeveloping your app, you should always assume that session affinity is not guaranteed.\nA client can lose affinity with the target instance in the following scenarios:\n\n- The App Engine autoscaler can add or remove instances that serve your application. The application might reallocate the load, and the target instance might move. To minimize this risk, ensure that you have set the minimum number of instances to handle the expected load.\n- If the target instance fails health checks, App Engine moves the session to a healthy instance. For more information about health checks and their customization options, see [Split health checks](/appengine/docs/flexible/reference/app-yaml#updated_health_checks).\n- Session affinity is lost when an instance is rebooted for maintenance or software updates. App Engine flexible environment VM instances are restarted on a weekly basis.\n\nBecause session affinity isn't guaranteed, you should only use it to take\nadvantage of the ability of `socket.io` and other libraries to fall back\non HTTP long polling in cases where the connection is broken. You should never\nuse session affinity to build stateful applications.\n\nEnabling and disabling session affinity\n---------------------------------------\n\n| **Important:** To take advantage of session affinity, cookies must be enabled for all clients that are accessing your application, such as end-user web browsers and API clients. Clients without cookie support cannot take advantage of session affinity.\n\nBy default, session affinity is disabled for all App Engine applications.\nSession affinity is set at the version level of your application and can be\nenabled or disabled on deployment.\n\nTo enable session affinity for your App Engine version, add the following\nentry to your `app.yaml` file: \n\n network:\n session_affinity: true\n\nOnce the version is deployed with the updated app.yaml, new requests will start\nserving from the same instance as long as that instance is available.\n\nTo turn off session affinity, remove the entry from your [`app.yaml` file](/appengine/docs/flexible/reference/app-yaml#network_settings),\nor set the value to false: \n\n network:\n session_affinity: false"]]