Code Monkey home page Code Monkey logo

arsw_2019-2_lab_7_websockets's Introduction

ARSW_2019-2_Lab_7_Websockets

Name :

Etienne Maxence Eugene REITZ
GitHub username : Tylones

Build and test instructions :

Go in the project directory :

  • To build the project , run the command : mvn package
  • To test the project, run the command : mvn test
  • To compile the project, run the command : mvn compile
  • To run the project, run the command : mvn spring-boot:run

The application is accessible at https://localhost:8080/

The laboratory

Backend

  • The controller with the shared ArrayList :
@Controller
public class AppPointController {

    private ArrayList<Point> points = new ArrayList<Point>();

    @Autowired
    SimpMessagingTemplate mgt;

    @MessageMapping("/newpoint.{id}")
    public void handlePointEvent(Point p, @DestinationVariable String id) throws Exception {
        synchronized (points){
            points.add(p);

            if(points.size()>=3){
                mgt.convertAndSend("/topic/newpolygon."+id, points);
                points.clear();
            }
        }

        mgt.convertAndSend("/topic/newpoint."+id, p);

    }


}

Frontend

  • Connecting and subscribing to the two topics, with their respective callback functions :
var connectAndSubscribe = function () {
        console.info('Connecting to WS...');
        var socket = new SockJS('/stompendpoint');
        stompClient = Stomp.over(socket);
        
        //subscribe to /topic/TOPICXX when connections succeed
        stompClient.connect({}, function (frame) {
            console.log('Connected: ' + frame);
            stompClient.subscribe('/topic/newpoint.'+drawingNumber, function (eventbody) {
                var x = (JSON.parse(eventbody.body)).x;
                var y = (JSON.parse(eventbody.body)).y;
                var point = new Point(x,y);
                addPointToCanvas(point);
            });
            
            stompClient.subscribe('/topic/newpolygon.'+drawingNumber, function (eventbody) {
                var canvas = document.getElementById("canvas");
                var ctx = canvas.getContext("2d");
                var point0 = null;
                ctx.fillStyle = getRndColor();
                ctx.beginPath();
                (JSON.parse(eventbody.body)).map(function (elem, index) {
                    if(index === 0){
                        point0 = elem;
                        ctx.moveTo(elem.x, elem.y);
                    }else{
                        ctx.lineTo(elem.x, elem.y);
                        if(index === (JSON.parse(eventbody.body)).length){
                            ctx.lineTo(poin0.x, point0.y);
                        }
                    }
                })
                ctx.closePath();
                ctx.fill();
            })
        });

    };

Screenshot of the final application

In this screenshot, we can see that the first to windows are connected to the drawing 1, and the last window is connected to the drawing 2.

As a result, the first two windows do have the same polygons (the colors are generated randomly when drawing, and are not shared), and the last window has different polygons.

final

arsw_2019-2_lab_7_websockets's People

Contributors

tylones avatar

Watchers

James Cloos 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.