I've bee开发者_运维技巧n looking at examples and I just cannot get how it works.
What I'm trying to do is pretty simple I would presume and hoping if someone could give me a hand?
I'm trying to have a server running that clients could connect to. Lets say 127.0.0.1:8126 And if someone connects using a query string e.g. 127.0.0.1:8126/? color=red then every client that is currently on the server would have the background of the page turn to red.
Could someone help me out with this?
Thanks so much! Tyler
for realtime updates you should use socket.io or the like.
client.html:
<!DOCTYPE html>
<title>test</title>
<script src="http://cdn.socket.io/stable/socket.io.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<input type=text>
<button>send css</button>
<script>
$(document).ready(function(){
var socket = new io.Socket();
socket.connect();
socket.on('message',function(msg){
$('body').css(msg);
});
$('button').click(function(){
socket.send(JSON.parse($('input').val()));
});
});
</script>
server.js
var fs=require("fs");
var clientpage = fs.readFileSync("client.html");
var http = require('http'),
io = require('socket.io');
var server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(clientpage);
});
server.listen(8126);
var socket = io.listen(server);
socket.on('connection', function(client){
client.on('message', function(data){
socket.broadcast(data);
});
});
It's a crude example since you need to type in the proper json for jquery.css() (eg {"background-color":"red"}) but it runs
link
精彩评论