How to import socket.io on client side - SOCKET.IO + NODE.JS
up vote
0
down vote
favorite
I have been trying to create an online game but when I try to use socket.on(...);
or socket.emit(...)
it comes up with an error like what is socket
. I have seen some posts similar to this one and tried all the solutions but none worked. I have already got io.connect(...);
working and it works (I assume this means I have properly set up socket.io-client). I just don't see what I am doing wrong. If you need the code you can simply request it though I don't think it was necessary. Thanks.
javascript html node.js websocket socket.io
add a comment |
up vote
0
down vote
favorite
I have been trying to create an online game but when I try to use socket.on(...);
or socket.emit(...)
it comes up with an error like what is socket
. I have seen some posts similar to this one and tried all the solutions but none worked. I have already got io.connect(...);
working and it works (I assume this means I have properly set up socket.io-client). I just don't see what I am doing wrong. If you need the code you can simply request it though I don't think it was necessary. Thanks.
javascript html node.js websocket socket.io
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have been trying to create an online game but when I try to use socket.on(...);
or socket.emit(...)
it comes up with an error like what is socket
. I have seen some posts similar to this one and tried all the solutions but none worked. I have already got io.connect(...);
working and it works (I assume this means I have properly set up socket.io-client). I just don't see what I am doing wrong. If you need the code you can simply request it though I don't think it was necessary. Thanks.
javascript html node.js websocket socket.io
I have been trying to create an online game but when I try to use socket.on(...);
or socket.emit(...)
it comes up with an error like what is socket
. I have seen some posts similar to this one and tried all the solutions but none worked. I have already got io.connect(...);
working and it works (I assume this means I have properly set up socket.io-client). I just don't see what I am doing wrong. If you need the code you can simply request it though I don't think it was necessary. Thanks.
javascript html node.js websocket socket.io
javascript html node.js websocket socket.io
asked Nov 13 at 5:15
Dragsaw Dragon
446
446
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
To use SocketIO on the client-side, first import it via
import * as io from 'socket.io-client';
Then, probably in a constructor, establish a connection to the server's ip and port running the SocketIO server
const socket = io(`172.16.1.3:81`);
Also in the constructor you will likely want to setup a function to be called when the server sends a certain message type:
socket.on("update-player", handlePlayerUpdateFunction);
and
function handlePlayerUpdateFunction(message) {
console.log(`Received message from server: ${message}`);
}
If this client does not receive the message sent from your server, then something is wrong with your server not being set up correctly to listen on the port.
I tried doing this but it says * is not defined...
– Dragsaw Dragon
Nov 13 at 7:55
the 'socket' variable is not defined? if so then something is wrong with your server and its not listening on the port
– Shawn Andrews
Nov 13 at 16:17
add a comment |
up vote
0
down vote
After trying some things and doing some research I found the solution to my problem. Adding in var socket = io.connect(x.x.x.x:3000);
my code started functioning and everything was perfect I had already imported socket.io-client trough CDNJS link on the installation guide. I just had to Initialise the variable.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
To use SocketIO on the client-side, first import it via
import * as io from 'socket.io-client';
Then, probably in a constructor, establish a connection to the server's ip and port running the SocketIO server
const socket = io(`172.16.1.3:81`);
Also in the constructor you will likely want to setup a function to be called when the server sends a certain message type:
socket.on("update-player", handlePlayerUpdateFunction);
and
function handlePlayerUpdateFunction(message) {
console.log(`Received message from server: ${message}`);
}
If this client does not receive the message sent from your server, then something is wrong with your server not being set up correctly to listen on the port.
I tried doing this but it says * is not defined...
– Dragsaw Dragon
Nov 13 at 7:55
the 'socket' variable is not defined? if so then something is wrong with your server and its not listening on the port
– Shawn Andrews
Nov 13 at 16:17
add a comment |
up vote
1
down vote
accepted
To use SocketIO on the client-side, first import it via
import * as io from 'socket.io-client';
Then, probably in a constructor, establish a connection to the server's ip and port running the SocketIO server
const socket = io(`172.16.1.3:81`);
Also in the constructor you will likely want to setup a function to be called when the server sends a certain message type:
socket.on("update-player", handlePlayerUpdateFunction);
and
function handlePlayerUpdateFunction(message) {
console.log(`Received message from server: ${message}`);
}
If this client does not receive the message sent from your server, then something is wrong with your server not being set up correctly to listen on the port.
I tried doing this but it says * is not defined...
– Dragsaw Dragon
Nov 13 at 7:55
the 'socket' variable is not defined? if so then something is wrong with your server and its not listening on the port
– Shawn Andrews
Nov 13 at 16:17
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
To use SocketIO on the client-side, first import it via
import * as io from 'socket.io-client';
Then, probably in a constructor, establish a connection to the server's ip and port running the SocketIO server
const socket = io(`172.16.1.3:81`);
Also in the constructor you will likely want to setup a function to be called when the server sends a certain message type:
socket.on("update-player", handlePlayerUpdateFunction);
and
function handlePlayerUpdateFunction(message) {
console.log(`Received message from server: ${message}`);
}
If this client does not receive the message sent from your server, then something is wrong with your server not being set up correctly to listen on the port.
To use SocketIO on the client-side, first import it via
import * as io from 'socket.io-client';
Then, probably in a constructor, establish a connection to the server's ip and port running the SocketIO server
const socket = io(`172.16.1.3:81`);
Also in the constructor you will likely want to setup a function to be called when the server sends a certain message type:
socket.on("update-player", handlePlayerUpdateFunction);
and
function handlePlayerUpdateFunction(message) {
console.log(`Received message from server: ${message}`);
}
If this client does not receive the message sent from your server, then something is wrong with your server not being set up correctly to listen on the port.
answered Nov 13 at 5:30
Shawn Andrews
711316
711316
I tried doing this but it says * is not defined...
– Dragsaw Dragon
Nov 13 at 7:55
the 'socket' variable is not defined? if so then something is wrong with your server and its not listening on the port
– Shawn Andrews
Nov 13 at 16:17
add a comment |
I tried doing this but it says * is not defined...
– Dragsaw Dragon
Nov 13 at 7:55
the 'socket' variable is not defined? if so then something is wrong with your server and its not listening on the port
– Shawn Andrews
Nov 13 at 16:17
I tried doing this but it says * is not defined...
– Dragsaw Dragon
Nov 13 at 7:55
I tried doing this but it says * is not defined...
– Dragsaw Dragon
Nov 13 at 7:55
the 'socket' variable is not defined? if so then something is wrong with your server and its not listening on the port
– Shawn Andrews
Nov 13 at 16:17
the 'socket' variable is not defined? if so then something is wrong with your server and its not listening on the port
– Shawn Andrews
Nov 13 at 16:17
add a comment |
up vote
0
down vote
After trying some things and doing some research I found the solution to my problem. Adding in var socket = io.connect(x.x.x.x:3000);
my code started functioning and everything was perfect I had already imported socket.io-client trough CDNJS link on the installation guide. I just had to Initialise the variable.
add a comment |
up vote
0
down vote
After trying some things and doing some research I found the solution to my problem. Adding in var socket = io.connect(x.x.x.x:3000);
my code started functioning and everything was perfect I had already imported socket.io-client trough CDNJS link on the installation guide. I just had to Initialise the variable.
add a comment |
up vote
0
down vote
up vote
0
down vote
After trying some things and doing some research I found the solution to my problem. Adding in var socket = io.connect(x.x.x.x:3000);
my code started functioning and everything was perfect I had already imported socket.io-client trough CDNJS link on the installation guide. I just had to Initialise the variable.
After trying some things and doing some research I found the solution to my problem. Adding in var socket = io.connect(x.x.x.x:3000);
my code started functioning and everything was perfect I had already imported socket.io-client trough CDNJS link on the installation guide. I just had to Initialise the variable.
answered Nov 13 at 23:46
Dragsaw Dragon
446
446
add a comment |
add a comment |
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%2f53274250%2fhow-to-import-socket-io-on-client-side-socket-io-node-js%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