Note that there are some explanatory texts on larger screens.

plurals
  1. POnode.js socket.io simple chat
    primarykey
    data
    text
    <p>I'm starting playing with node.js and as everybody, I want do a chat.</p> <p>My idea is run node.js with socket.io in the port 9090, for example, and my client html in the port 8080. My html client will be served independent.</p> <p>My server:</p> <pre><code>var sys = require('sys'); var express = require('express'); var io = require('socket.io'); var app = express.createServer(); app.listen(8080); var socket = io.listen(app); socket.on('connection', function (client) { client.on('message', function (msg) { socket.broadcast(msg); }); client.on('disconnect', function () { }); }); </code></pre> <p>My client:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="http://cdn.socket.io/stable/socket.io.js"&gt;&lt;/script&gt; &lt;script&gt; $(document).ready(function () { var socket = new io.Socket("localhost", {port: 8080}); socket.on('connect', function () { socket.send('A client connected.'); }); socket.on('message', function (message) { $('div#messages').append($('&lt;p&gt;'), message); }); socket.on('disconnect', function () { console.log('disconnected'); }); socket.connect(); $('input').keydown(function (event) { if(event.keyCode === 13) { socket.send($('input').val()); $('input').val(''); } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input type="text" style="width: 300px;" /&gt; &lt;div id="messages" style="border:solid 1px #000;"&gt;&amp;nbsp;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I'm running in ubuntu 11.04 with node.js v0.4.10.</p> <p>The server works fine, but the client can't do connection, in the console.log on google Chrome I received this message: XMLHttpRequest cannot load <a href="http://localhost:8080/socket.io/xhr-polling//1311465961485" rel="noreferrer">http://localhost:8080/socket.io/xhr-polling//1311465961485</a>. Origin <a href="http://localhost" rel="noreferrer">http://localhost</a> is not allowed by Access-Control-Allow-Origin.</p> <p>The server.js is in a folder in /var/www/cliente/chat/public.</p> <p>What's the problem?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload