Giter Club home page Giter Club logo

websocket-example's Introduction

WebSocket Example Implementation

This repository contains a simple WebSocket-based chat example built with the Fat-Free Framework and HTML5.

Contents

Features

  • Provide a WebSocket server
  • Log every \CLI\WS event
  • Notify clients that users joined or left the chat
  • Receive chat messages
  • Forward chat messages to all clients

Implementation

The WebSocket server implementation consists of two files:

  • /bin/websocket-server.php configures and starts the chat backend

  • /src/App/WebSocketServer.php registers listeners and implements the application-specific features

Run & Chat

  1. Install Fat-Free Framework

    composer install --no-dev
  2. Start the WebSocket server

    ./bin/websocket-server.php tcp://127.0.0.1:9001
  3. Start PHP's built-in server or the server of your choice and make the /public directory accessible via HTTP

    cd public && php -S 127.0.0.1:9000
  4. Visit the HTML5-based chat client, e.g. http://127.0.0.1:9000. The web client will automatically connect to the same host on port 9001 (e.g. ws://127.0.0.1:9001, see /public/index.html). As soon as the connection is ready, it is possible to chat with other visitors.

Example Output

The \App\WebSocketServer class listens to all \CLI\WS events and logs the results.

WebSocket Server

2020-04-04 20:17:26 | 2.097 MB | WebSocket server started
2020-04-04 20:17:37 | 2.097 MB | Agent 127.0.0.1:42478 connected
2020-04-04 20:17:41 | 2.097 MB | Agent 127.0.0.1:42480 connected
2020-04-04 20:17:41 | 2.097 MB | Agent 127.0.0.1:42478 will receive a message: Client with ID 127.0.0.1:42480 joined
2020-04-04 20:17:53 | 2.097 MB | Agent 127.0.0.1:42478 sent a message: Hello World
2020-04-04 20:17:53 | 2.097 MB | Forward message to all clients: {"author":"127.0.0.1:42478","message":"Hello World"}
2020-04-04 20:17:53 | 2.097 MB | Agent 127.0.0.1:42478 will receive a message: {"author":"127.0.0.1:42478","message":"Hello World"}
2020-04-04 20:17:53 | 2.097 MB | Agent 127.0.0.1:42480 will receive a message: {"author":"127.0.0.1:42478","message":"Hello World"}
2020-04-04 20:18:05 | 2.097 MB | Agent 127.0.0.1:42480 disconnected
2020-04-04 20:18:05 | 2.097 MB | Agent 127.0.0.1:42478 will receive a message: Client with ID 127.0.0.1:42480 left
2020-04-04 20:18:15 | 2.097 MB | Agent 127.0.0.1:42478 disconnected
2020-04-04 20:18:15 | 2.097 MB | Shutting down

WebSocket Client

Connect to ws://127.0.0.1:9001
WebSocket.onopen() called
Client with ID 127.0.0.1:42480 joined
{"author":"127.0.0.1:42478","message":"Hello World"}
Client with ID 127.0.0.1:42480 left
WebSocket.onclose() called

websocket-example's People

Contributors

rayne avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

websocket-example's Issues

WebSocket example code doesn't work

I can connect to the WebSocket server with clients in a terminal and with index.html in a browser. But I can't send or receive messages. In the terminal with the running Websocket server, every minute I see:
2023-10-14 18:22:40 | 4.194 MB | Agent 127.0.0.1:58920 idles

Nothing more.
In the browser:

Connect to ws://127.0.0.1:9001
WebSocket.onopen() called

How to make it work?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.