Created
January 12, 2022 14:34
-
-
Save maxmonax/62170484338d69db4a75f81fd59b448f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var io; | |
var socket; | |
module.exports.initGame = function(aIO, aSocket) { | |
console.log('initGame...'); | |
io = aIO; | |
socket = aSocket; | |
if (!io) { | |
console.log('io == null!'); | |
return; | |
} | |
if (!aSocket) { | |
console.log('socket == null!'); | |
return; | |
} | |
socket.on('ping', onPing); | |
return; | |
var id = Math.trunc(Math.random() * 999); | |
var userName = 'user_' + id; | |
console.log(userName + ' connected'); | |
socket.broadcast.emit('chat msg', 'User ' + userName + ' connected...'); | |
socket.on('chat msg', (msg) => { | |
console.log('message: ' + msg); | |
io.emit('chat msg', userName + ': ' + msg); | |
}); | |
socket.on('disconnect', () => { | |
socket.broadcast.emit('chat msg', 'User ' + userName + ' disconnected...'); | |
console.log(userName + ' disconnected'); | |
}); | |
} | |
function onPing() { | |
socket.emit('pong'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
const server = require('http').createServer(app); | |
const io = require('socket.io')(server); | |
const HOST = '127.0.0.1'; | |
const PORT = process.env.port || '3000'; | |
server.listen(PORT, () => { | |
console.log(`Server started at http://${HOST}:${PORT}`); | |
}); | |
var game = require('./logic/mcpgame'); | |
io.on('connection', (socket) => { | |
game.initGame(io, socket); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment