I have written code for socket from :source
My code looks like this.
var port = 8081;
var app = require('http').createServer();
var io = require('socket.io')(app);
app.listen(port);
io.on('connection', function (socket) {
console.log("New user connected.");
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
console.log("server listening on: " + port);
But when I am testing it on here (ws://localhost:8081). I am getting
ERROR: undefined
DISCONNECTED
In console I am getting error:
WebSocket connection to 'ws://localhost:8081/?encoding=text' failed: Connection closed before receiving a handshake response
You need to use the socket.io client library, either getting it, from here, or referencing their CDN from your page like this:
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
Additionally, whenever you start a socket.io server, socket.io will be served from http://<your-server-address>/socket.io/socket.io.js. You should be able to test your server using this simple page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://localhost:8081/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost:8081/');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</head>
<body>
</body>
</html>
You can read more about why you can't connect from the websocket.org site on this github issue.
Related
I'm just getting started again with Node and Socket IO. I'm using socket.io v1.3.7.
I can load the socket IO client but it is long polling instead of using sockets.
The last time I used socketIO it worked fine. I think that was the 0.9.x branch.
http://sockettest.dev:3000/socket.io/?EIO=3&transport=polling&t=1447350154198-2&sid=eW7B_kpSat6WfGQSAAAC
Here's the code..
Server - app.js
var server = require('http').createServer();
var io = require('socket.io')(server);
io.on('connection', function(socket) {
socket.emit('connection', 'connected');
socket.on('disconnect', function() {
socket.emit('disconnection', 'not connected');
});
});
server.listen(3000);
Client - index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="//recently.dev:3000/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('<?php echo '//' . $_SERVER['HTTP_HOST'] . ':3000/'; ?>');
socket.on('connection', function(data){
console.log(data);
});
</script>
</head>
<body>
</body>
</html>
I think this is why..
Why is my socket.io using long polling instead of the websocket?
I think older approach was better IMO. There should at least be an option.
I have nodeJs application which is running on server http://mysite.co:8081. Where I have chat application. Using socket.io for communication.
Now I want to connect to socket from localhost -- I am trying to connect from my machine where I have wamp installed and in that created one html file in www/test/index.html.
The index.html have below code only.
<script src="http://mysite.co:8081/socket.io/socket.io.js"></script>
<script>
var socket = io('http://mysite.co:8081');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
The socket.io.js file is loading correctly, But I am getting below error
Uncaught TypeError: io is not a function
Let me now what I am doing wrong.
Thanks,
try this one:
<script src="http://mysite.co:8081/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://mysite.co:8081');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
I’m having a problem getting started with Node.js.
I’ve created a basic server that I know works, because if I navigate to http://localhost:5000 in my browser I get the expected message. However, I’m having trouble then connecting to this server on the client side with a basic HTML page.
My Node.js app looks like this:
var http = require('http');
var socket = require('socket.io');
var port = process.env.PORT || 5000;
var players;
var app = http.createServer(function(request, response) {
response.write('Server listening to port: ' + port);
response.end();
}).listen(port);
var io = socket.listen(app);
function init() {
io.configure(function() {
io.set('transports', [ 'xhr-polling' ]);
io.set('polling duration', 10);
});
io.sockets.on('connection', onSocketConnection);
};
function onSocketConnection(client) {
console.log('New connection');
console.log(client);
};
init();
My HTML page looks like this (based on https://github.com/mongolab/tractorpush-server/blob/master/index.html):
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('/');
socket.on('all', function(data) {
console.log(data);
});
socket.on('complex', function(data) {
console.log(data);
});
</script>
</body>
</html>
I understand that the sockets.io.js file is automatically generated by socket.io, but I just get the following error when I view my index.html file:
Uncaught ReferenceError: io is not defined
How do I actually connect to my server?
today I follow an tutorial by Gonzalo Ayuso at http://gonzalo123.com/2011/05/23/real-time-notifications-part-ii-now-with-node-js-and-socket-io/ but it can't send the message
Here is my server.js
var http = require('http');
var io = require('socket.io');
server = http.createServer(function(req, res){
});
server.listen(8000);
//socket.io
var socket = io.listen(server);
socket.set('transports', ['websocket']);
console.log("Start");
socket.on('connection', function(client){
client.on('message', function(msg){
console.log(msg);
socket.broadcast(msg);
})
});
and the client.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Comet Test</title>
</head>
<body>
<p><a id='customAlert' href="#" onclick='socket.send("customAlert")'>publish customAlert</a></p>
<p><a id='customAlert2' href="#" onclick='socket.send("customAlert2")'>publish customAlert2</a></p>
<script src="http://localhost:8000/socket.io/socket.io.js" type="text/javascript"></script>
<script type="text/javascript">
// Start the socket
var socket = io.connect('http://localhost:8000');
socket.on('message', function(msg){
console.log(msg);
});
</script>
</body>
</html>
I have edited it just a little bit to run on my server. But the client doesn't send message to server. Can anybody help me? Sorry for my bad English.
I have found out that the client can't connect to server but I don't know why?
My computer is running xampp with apache server. Maybe it's problem?
Updated:
I have just set transports to xhr-polling and it connect success. Why doesn't it accept websocket?
you need to read more carefully the tutorials about socket.io
first of all to call some event, you need to emit the event name:
socket.emit('my other event', { my: 'data' });
thats mean, on your onclick in anchor need to call socket.emit ..., or to call some function like:
function call_my_event(name){
socket.emit(name,{my : 'data'});
}
I have found out my problem. It's about firewall or proxy or something else in my computer block websocket protocol. I tested that code on another computer and it works fine. So I have to re-install my system and websocket is available for me :D
I am having some problems in executing my first socket.io program. Here is the code
var io=require('socket.io');
var http=require('http');
server =http.createServer(function(req,res) {
res.writeHead(200,{'Content-Type':'text/html'});
res.write('welcome');
res.end();
});
server.listen(7777);
var socket = io.listen(server);
socket.on('connection', function (client) {
console.log('client connected');
client.emit('news', { hello: 'world' });
client.on('another', function (data) {
console.log(data);
});
});
And here is my client side code
<html>
<head>
<script type="text/javascript" src="http://localhost:7777/socket.io/lib/socket.io.js"> </script>
<script type="text/javascript">
var socket = io.connect("http://localhost:7777");
socket.on('news',function(data) {
alert(data);
socket.emit('another',{my:'data'});
});
</script>
</head>
</html>
EDIT: Now, i get an error in my server terminal "info - client protocol unsupported"
what am i doing wrong here? Thanks
You are trying to import a javascript file from the socket io port instead of the web server port.
<script type="text/javascript" src="http://localhost:7777/socket.io/lib/socket.io.js"> </script>
You need to give the correct src parameter. For newer version of socket io, this would be something like socket/socket.io.js.