Git Product home page Git Product logo

hello_websocket's Introduction

Hello_WebSocket

My first websocket practice!

What does it do

Backend: wsserver.js

  • Create the websocket server and wait for connections
  • Forward the message to other clients when a message is received
  • Establish a websocket with the server
  • Check if the connection is successful
  • Send messages to the server

Knowledge involved

Backend

  • Basic node.js
  • ws library
  • Message forwarding techniques

Frontend

  • Basic jQuery grammar
  • How to create websockets
  • How to manipulate elements with jQuery statements

Study notes

  1. In html, id is case-sensitive!!!
  2. To use jQuery, add <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.min.js"></script> (You may change the link to other CDNs) before other code.
  3. What does $(function() {*}); do? The function inside is executed when the page is ready. (https://stackoverflow.com/questions/7642442/what-does-function-do)
  4. I learned this from https://medium.com/@martin.sikora/node-js-websocket-simple-chat-tutorial-2def3a841b61
  5. If the user is using mozilla, we should use its built-in websocket. To use the apporiate websocket, we can do it like this:
window.WebSocket = window.WebSocket || window.MozWebSocket;
  1. To create the websocket:
var conn = new WebSocket("ws://" + location.hostname + ":8080");
  1. You can use $(this) within an event handler for an element.
  2. The websocket server should not forward the message to the client who sent the message. To do this:
wss.clients.forEach(function(client) {
    if (client !== ws && client.readyState === websocket.OPEN) {
        client.send(data);
    }
});

hello_websocket's People

Contributors

sweeticelolly avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

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.