JS + Socket.io + Heroku Problems - Socket.id problem
Solution
Change
var server = app.listen(3000);
To
var server = app.listen(process.env.PORT || 5000);
I want to deploy an game made with JavaScript on Heroku.
Here's my server:
var express = require('express');
var app = express();
var server = app.listen(3000);
var socket = require('socket.io');
var io = socket(server);
app.use(express.static('public'));
var connectedPlayers = {};
console.log("Server is running!");
io.on('connection',
function (socket) {
socket.on('newPlayer',
function (data) {
console.log("New player connected - ID: " + data.id);
connectedPlayers[socket.id] = {
idOnline: socket.id,
idOffline: data.id,
x: data.w,
y: data.h
};
socket.emit('allPlayers', connectedPlayers);
socket.broadcast.emit('newPlayer', connectedPlayers[socket.id]);
});
socket.on('move',
function (data) {
connectedPlayers[socket.id].x = data.x;
connectedPlayers[socket.id].y = data.y;
socket.broadcast.emit('move', connectedPlayers[socket.id]);
});
socket.on('message',
function (data) {
message = {
name: data.name,
message: data.message,
messageId: generateId()
};
socket.broadcast.emit('message', message);
});
socket.on('emote',
function (data) {
message = {
emote: data.emote,
id: socket.id
}
socket.broadcast.emit('emote', message);
});
socket.on('disconnect', function () {
delete connectedPlayers[socket.id];
io.emit('remove', socket.id);
});
});
This work's fine locally, but when I deploy to heroku I get this error message:
2018-11-23T21:04:18.009491+00:00 app[web.1]: /app/server.js:33
2018-11-23T21:04:18.009512+00:00 app[web.1]: connectedPlayers[socket.id].x = data.x;
2018-11-23T21:04:18.009514+00:00 app[web.1]: ^
2018-11-23T21:04:18.009516+00:00 app[web.1]:
2018-11-23T21:04:18.009518+00:00 app[web.1]: TypeError: Cannot set property 'x' of undefined
I understand that heroku is not recognizing the "connectedPlayers" array at that index, but how this can work properly locally?
What's wrong with the socket.id property?
PS.: the socket.id it's sended by the client, but I think that's generated after a client establish an connection with the server right?
javascript heroku socket.io
add a comment |
Solution
Change
var server = app.listen(3000);
To
var server = app.listen(process.env.PORT || 5000);
I want to deploy an game made with JavaScript on Heroku.
Here's my server:
var express = require('express');
var app = express();
var server = app.listen(3000);
var socket = require('socket.io');
var io = socket(server);
app.use(express.static('public'));
var connectedPlayers = {};
console.log("Server is running!");
io.on('connection',
function (socket) {
socket.on('newPlayer',
function (data) {
console.log("New player connected - ID: " + data.id);
connectedPlayers[socket.id] = {
idOnline: socket.id,
idOffline: data.id,
x: data.w,
y: data.h
};
socket.emit('allPlayers', connectedPlayers);
socket.broadcast.emit('newPlayer', connectedPlayers[socket.id]);
});
socket.on('move',
function (data) {
connectedPlayers[socket.id].x = data.x;
connectedPlayers[socket.id].y = data.y;
socket.broadcast.emit('move', connectedPlayers[socket.id]);
});
socket.on('message',
function (data) {
message = {
name: data.name,
message: data.message,
messageId: generateId()
};
socket.broadcast.emit('message', message);
});
socket.on('emote',
function (data) {
message = {
emote: data.emote,
id: socket.id
}
socket.broadcast.emit('emote', message);
});
socket.on('disconnect', function () {
delete connectedPlayers[socket.id];
io.emit('remove', socket.id);
});
});
This work's fine locally, but when I deploy to heroku I get this error message:
2018-11-23T21:04:18.009491+00:00 app[web.1]: /app/server.js:33
2018-11-23T21:04:18.009512+00:00 app[web.1]: connectedPlayers[socket.id].x = data.x;
2018-11-23T21:04:18.009514+00:00 app[web.1]: ^
2018-11-23T21:04:18.009516+00:00 app[web.1]:
2018-11-23T21:04:18.009518+00:00 app[web.1]: TypeError: Cannot set property 'x' of undefined
I understand that heroku is not recognizing the "connectedPlayers" array at that index, but how this can work properly locally?
What's wrong with the socket.id property?
PS.: the socket.id it's sended by the client, but I think that's generated after a client establish an connection with the server right?
javascript heroku socket.io
add a comment |
Solution
Change
var server = app.listen(3000);
To
var server = app.listen(process.env.PORT || 5000);
I want to deploy an game made with JavaScript on Heroku.
Here's my server:
var express = require('express');
var app = express();
var server = app.listen(3000);
var socket = require('socket.io');
var io = socket(server);
app.use(express.static('public'));
var connectedPlayers = {};
console.log("Server is running!");
io.on('connection',
function (socket) {
socket.on('newPlayer',
function (data) {
console.log("New player connected - ID: " + data.id);
connectedPlayers[socket.id] = {
idOnline: socket.id,
idOffline: data.id,
x: data.w,
y: data.h
};
socket.emit('allPlayers', connectedPlayers);
socket.broadcast.emit('newPlayer', connectedPlayers[socket.id]);
});
socket.on('move',
function (data) {
connectedPlayers[socket.id].x = data.x;
connectedPlayers[socket.id].y = data.y;
socket.broadcast.emit('move', connectedPlayers[socket.id]);
});
socket.on('message',
function (data) {
message = {
name: data.name,
message: data.message,
messageId: generateId()
};
socket.broadcast.emit('message', message);
});
socket.on('emote',
function (data) {
message = {
emote: data.emote,
id: socket.id
}
socket.broadcast.emit('emote', message);
});
socket.on('disconnect', function () {
delete connectedPlayers[socket.id];
io.emit('remove', socket.id);
});
});
This work's fine locally, but when I deploy to heroku I get this error message:
2018-11-23T21:04:18.009491+00:00 app[web.1]: /app/server.js:33
2018-11-23T21:04:18.009512+00:00 app[web.1]: connectedPlayers[socket.id].x = data.x;
2018-11-23T21:04:18.009514+00:00 app[web.1]: ^
2018-11-23T21:04:18.009516+00:00 app[web.1]:
2018-11-23T21:04:18.009518+00:00 app[web.1]: TypeError: Cannot set property 'x' of undefined
I understand that heroku is not recognizing the "connectedPlayers" array at that index, but how this can work properly locally?
What's wrong with the socket.id property?
PS.: the socket.id it's sended by the client, but I think that's generated after a client establish an connection with the server right?
javascript heroku socket.io
Solution
Change
var server = app.listen(3000);
To
var server = app.listen(process.env.PORT || 5000);
I want to deploy an game made with JavaScript on Heroku.
Here's my server:
var express = require('express');
var app = express();
var server = app.listen(3000);
var socket = require('socket.io');
var io = socket(server);
app.use(express.static('public'));
var connectedPlayers = {};
console.log("Server is running!");
io.on('connection',
function (socket) {
socket.on('newPlayer',
function (data) {
console.log("New player connected - ID: " + data.id);
connectedPlayers[socket.id] = {
idOnline: socket.id,
idOffline: data.id,
x: data.w,
y: data.h
};
socket.emit('allPlayers', connectedPlayers);
socket.broadcast.emit('newPlayer', connectedPlayers[socket.id]);
});
socket.on('move',
function (data) {
connectedPlayers[socket.id].x = data.x;
connectedPlayers[socket.id].y = data.y;
socket.broadcast.emit('move', connectedPlayers[socket.id]);
});
socket.on('message',
function (data) {
message = {
name: data.name,
message: data.message,
messageId: generateId()
};
socket.broadcast.emit('message', message);
});
socket.on('emote',
function (data) {
message = {
emote: data.emote,
id: socket.id
}
socket.broadcast.emit('emote', message);
});
socket.on('disconnect', function () {
delete connectedPlayers[socket.id];
io.emit('remove', socket.id);
});
});
This work's fine locally, but when I deploy to heroku I get this error message:
2018-11-23T21:04:18.009491+00:00 app[web.1]: /app/server.js:33
2018-11-23T21:04:18.009512+00:00 app[web.1]: connectedPlayers[socket.id].x = data.x;
2018-11-23T21:04:18.009514+00:00 app[web.1]: ^
2018-11-23T21:04:18.009516+00:00 app[web.1]:
2018-11-23T21:04:18.009518+00:00 app[web.1]: TypeError: Cannot set property 'x' of undefined
I understand that heroku is not recognizing the "connectedPlayers" array at that index, but how this can work properly locally?
What's wrong with the socket.id property?
PS.: the socket.id it's sended by the client, but I think that's generated after a client establish an connection with the server right?
javascript heroku socket.io
javascript heroku socket.io
edited Nov 23 '18 at 21:48
Jorge Nachtigall
asked Nov 23 '18 at 21:31
Jorge NachtigallJorge Nachtigall
154
154
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
After looking for a lot of solutions, I was trying crazy things to insert the:
process.env.PORT || 5000
on the server.listen.
As you can see in the code of the question, I've posted an out of date code, with the following line (my first try):
var server = app.listen(3000);
The solution was simplier than I thought, just changing the above line of code to:
var server = app.listen(process.env.PORT || 5000);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453144%2fjs-socket-io-heroku-problems-socket-id-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
After looking for a lot of solutions, I was trying crazy things to insert the:
process.env.PORT || 5000
on the server.listen.
As you can see in the code of the question, I've posted an out of date code, with the following line (my first try):
var server = app.listen(3000);
The solution was simplier than I thought, just changing the above line of code to:
var server = app.listen(process.env.PORT || 5000);
add a comment |
After looking for a lot of solutions, I was trying crazy things to insert the:
process.env.PORT || 5000
on the server.listen.
As you can see in the code of the question, I've posted an out of date code, with the following line (my first try):
var server = app.listen(3000);
The solution was simplier than I thought, just changing the above line of code to:
var server = app.listen(process.env.PORT || 5000);
add a comment |
After looking for a lot of solutions, I was trying crazy things to insert the:
process.env.PORT || 5000
on the server.listen.
As you can see in the code of the question, I've posted an out of date code, with the following line (my first try):
var server = app.listen(3000);
The solution was simplier than I thought, just changing the above line of code to:
var server = app.listen(process.env.PORT || 5000);
After looking for a lot of solutions, I was trying crazy things to insert the:
process.env.PORT || 5000
on the server.listen.
As you can see in the code of the question, I've posted an out of date code, with the following line (my first try):
var server = app.listen(3000);
The solution was simplier than I thought, just changing the above line of code to:
var server = app.listen(process.env.PORT || 5000);
answered Nov 23 '18 at 21:45
Jorge NachtigallJorge Nachtigall
154
154
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453144%2fjs-socket-io-heroku-problems-socket-id-problem%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown