{"id":21006231,"url":"https://github.com/derlin/pyton3-websocket-server-template","last_synced_at":"2025-06-17T23:02:56.588Z","repository":{"id":18702699,"uuid":"21913007","full_name":"derlin/pyton3-websocket-server-template","owner":"derlin","description":"A sample cherrypy server with ws4py websockets","archived":false,"fork":false,"pushed_at":"2016-12-18T09:05:07.000Z","size":17,"stargazers_count":4,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-12T07:39:13.486Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/derlin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-16T18:53:34.000Z","updated_at":"2024-06-22T02:46:04.000Z","dependencies_parsed_at":"2022-09-25T01:51:45.575Z","dependency_job_id":null,"html_url":"https://github.com/derlin/pyton3-websocket-server-template","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/derlin/pyton3-websocket-server-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derlin%2Fpyton3-websocket-server-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derlin%2Fpyton3-websocket-server-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derlin%2Fpyton3-websocket-server-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derlin%2Fpyton3-websocket-server-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/derlin","download_url":"https://codeload.github.com/derlin/pyton3-websocket-server-template/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/derlin%2Fpyton3-websocket-server-template/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260453716,"owners_count":23011572,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T08:50:01.009Z","updated_at":"2025-06-17T23:02:51.566Z","avatar_url":"https://github.com/derlin.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\npyton3-websocket-server-template\n================================\n\nA sample cherrypy server with ws4py websockets.\n\nPurpose\n-------\nThis project is just a skeleton to build a simple client-server application using websockets.\n\nIt is useful for very small projects which only requires a couple of pages and a fast deployement.\n\n\nHow to use\n----------\n\n### First run\n\n1. Run `setup.py install`\n2. Configure the server with your ip address and port (localhost:42000 by default)\n3. Run `python3 server.py`\n4. Open your webrowser (and its console \u003cF12\u003e) to check that everything works : `localhost:42000/index`\n\n### Editing\nModify the server parameters at the end of `server.py`, as well as the @exposed methods to suit your needs.\n\nWrite (or modify) a wscontroller, which handles incoming messages from the client sockets. The names of the methods should match the \"type\" of the incoming messages.\n\nWrite (or modify) the javascript file, which handles the client socket.\n\n\n\nHow it works\n------------\n\n### Messages format\nAll the messages exchanged are json-encoded and have the following structure:\n\n```python\n{   \n    type: \"a_message_type\",\n    data: \u003cwhatever you want\u003e\n}\n```\n\n### Server-side\nAn instance of the `GenericWebSocket` class will be created for each new connection. In the ws method of the server, you need to call the `set_controller`method to define the object which will handle incoming messages:\n\n```python\nclass SimpleServer( object ):\n    controller = WsController()\n    \n    @cherrypy.expose\n    def ws(self):\n        \"\"\"\n        Client socket should connect to ws://\u003chost\u003e:\u003cport\u003e/ws.\n        \"\"\"\n        # you can access the class instance through the following:\n        handler = cherrypy.request.ws_handler\n        handler.set_controller( self.controller )\n        # register the socket to the controller (for broadcasts)\n        self.controller.register_client_socket( self )\n        log.info( self, 'client socket registered' )\n        # don't forget to unregister the socket on close\n        handler.set_close_callback( lambda: self.controller.unregister_client_socket( handler ) )\n```\n\nUpon reception of a message, the socket will extract the message type and call the corresponding method of the controller (the names should match exactly), passing it the data and a reference on the socket. \nIf none is defined, the message will be ignored. A controller method has the following signature:\n\n```python\ndef a_message_type(self, socket, data):\n    # do something\n    pass\n```\n\n### Client-side\n\nIn the javascript, you should first create a `SimpleSocketHandler`:\n```javascript\n/** the server address, in the form ws//\u003chost\u003e:\u003cport\u003e/\u003ccherrypy exposed method\u003e */\nvar SERVER_ADDR = 'ws://localhost:42000/ws';\n\n// create the socket and connect to the server\nsocket = new SimpleSocketHandler(SERVER_ADDR);\n```\n\nYou can then use its `bind(message_type, function)` method to associate a callback to an incoming message:\n\n```javascript\n// register an event on socket opened\nsocket.bind(MessageTypes.WS_OPEN, function () {\n    console.log(\"opened\");\n    connected = true;\n});\n```\n\nTo send messages, call `socket.send(message_type, data)`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderlin%2Fpyton3-websocket-server-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fderlin%2Fpyton3-websocket-server-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fderlin%2Fpyton3-websocket-server-template/lists"}