canvasengine
v1.3.0
Published
Use a model for handling server-side events and develop a multiplayer game.
Downloads
3
Readme
Multiplayer
CanvasEngine implements a multiplayer game using NodeJS and Socket.io
Open the Shell :
npm install canvasengine
Create a. Js file in your project for the server
Here is an example of a client code :
<!DOCTYPE html>
<html>
<head>
<script src="canvasengine-X.Y.Z.all.min.js"></script>
<script src="extends/Socket.js"></script>
<script>
var Model = io.connect('http://127.0.0.1:8333');
var canvas = CE.defines("canvas").
ready(function() {
canvas.Scene.call("MyScene");
});
canvas.Scene.new({
name: "MyScene",
model: Model,
events: ["load"],
ready: function(stage) {
this.model.emit("start");
},
load: function(text) {
console.log(text);
}
});
</script>
</head>
<body>
<canvas id="canvas" width="675px" height="506px"></canvas>
</body>
</html>
And server code :
var CE = require("canvasengine").listen(8333);
CE.Model.init("Main", ["start"], {
initialize: function() {
},
start: function() {
this.scene.emit("load", "Hello");
}
});
- The client sends data to the server with the
emit
methodmodel
property - The method defined in the array is performed (method name sent by the client)
- Conversely, data is sent to the scene with the
emit
methodscene
property - Here, the
load
method is executed and defined in theevents
property
The methods are the same as Socket.io