Skip to content

Displays the connection status and ping via a websocket connection. No server setup or configuration required

License

Notifications You must be signed in to change notification settings

Protonosgit/MMM-PingTime

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MMM-PingTime

A module for the MagicMirror which displays the ping duration to websocket.org or a Custom websocket echo server in ms.

Previews

disconnected

pinging

Setup

No dependencies required !!
Just download the module and restart MM
DONE :)

cd ~/MagicMirror/modules 
git clone https://github.com/Protonosgit/MMM-PingTime

Configuration

Add the module configuration to your config.js file.

{
  module: 'MMM-PingTime',
  position: 'top_center',
  headder:'Network',
  config: {
    // See options bellow :)

    server:{
      // See: Host your own ping server
    }
  }
},

Config Options

Everthing here is optional!!!

Option Description Default Required
updateInterval The interval in which ping requests are sent 4000 (4 sec) False
startupDelay Defines the delay before a connection is made 2000 (2 sec) False
timePrefix The string which will be displayed before the ping False
timeSuffix The string which will be displayed after the ping ms False
connectedText The text which is shown after connecting Connected False
disconnectedText The text which is shown while offline Disconnected False
pingFontColor Changes the default text color --color-text False
connectedFontCollor Changes the default text color --color-text False
disconnectedFontColor Changes the default text color --color-text False
fontSize Changes the default font size 1 False
loadingIcon Changes the default loading indicator loading-icon fas fa-sync fa-spin False
connectedIcon Changes the default icon success-icon fas fa-check False
disconnectedIcon Changes the default icon warning-icon fas fa-ban False

Host your own ping server

By default this module requires an echo server which returns the same message sent by the client
Once the client sends the string: ping to the server, the same message should be returned.
However if you wish to change this behaviour you can do so. The official websocket source should work fine though (in most cases).

Everything here goes into the server tag INSIDE the config!!

Server Option Description Default Required
websocketUrl Defines the server url wss://echo.websocket.org False
outgoingMessage Defines the message sent in an interval ping False
incomingMessage Defines the expected response (diffenrent responses will be ignored) ping False
bypassCheck Ignores the incommingMessage check above False False

Python

import asyncio
import websockets

async def handle_connection(websocket, path):
    try:
        async for message in websocket:
            if message.lower() == "ping":
                await websocket.send("ping")
    except websockets.exceptions.ConnectionClosedError:
        pass

async def main():
    server = await websockets.serve(handle_connection, "localhost", 8765)
    await server.wait_closed()

if __name__ == "__main__":
    asyncio.run(main())

Javascript

const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 8765 });

wss.on('connection', function connection(ws) {

  ws.on('message', function incoming(message) {
    console.log('Ping received: %s', message);

    if (message.toString().toLowerCase() === 'ping') {
      ws.send('ping');
    }
  });

});

Contributions

are always wellcome so feel free to participate! : )