javascript - include socket.io server into my html -
i have run nodeserver , client sucessfully. want connect server proxy. here node client:
<html> <head> <script src="http://localhost:8000/socket.io/socket.io.js"></script> <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script> <script> var name = ''; var socket = io.connect('http://localhost:8000'); // @ document read (runs ones). $(document).ready(function(){ // on click of button (jquery thing) // things inside clause happen when // button clicked. $("button").click(function(){ // simple logging $("p#log").html('sent message: ' + $("input#msg").val()); // send message on inputbox server socket.emit('chat', $("input#msg").val() ); // server recieve message, // maybe processing, // broadcast again. however, not // send original sender. sender // browser sends msg. // other browsers listening server // recieve emitted message. therefore // need manually print msg sender. $("p#data_recieved").append("<br />\r\n" + name + ': ' + $("input#msg").val()); // empty text on input box. $("input#msg").val(''); }); // ask name of user, ask again if no name. while (name == '') { name = prompt("what's name?",""); } // send name server, , server's // register wait recieve this. socket.emit('register', name ); }); // listen chat event , recieve data socket.on('chat', function (data) { // print data (jquery thing) $("p#data_recieved").append("<br />\r\n" + data.msgr + ': ' + data.msg); // log event fun :d $("p#log").html('got message: ' + data.msg); }); </script> </head> <body> <input type="text" id="msg"></input><button>click me</button> <p id="log"></p> <p id="data_recieved"></p> </body> </html>
here server:
var check="check"; var io = require('socket.io').listen(8000); // open socket connection io.sockets.on('connection', function (socket) { // listen chat even. , recieve // data sender. console.log('hello nahid'); socket.on('chat', function (data) { // default value of name of sender. var sender = 'unregistered'; check=sender; // name of sender socket.get('nickname', function (err, name) { console.log('chat message ', name); console.log('error ', err); sender = name; }); // broadcast data recieved sender // others connected, not // original sender. socket.broadcast.emit('chat', { msg : data, msgr : sender }); }); // listen user registrations // set socket nickname socket.on('register', function (name) { // make nickname paramater socket // , set value name recieved // register above. , run // function follows inside it. socket.set('nickname', name, function () { // kind of emit send all! :d io.sockets.emit('chat', { msg : "naay nag apil2! si " + name + '!', msgr : "mr. server" }); }); }); });
they work together. want have included server file html client this:
<script type="text/javascript" src="nodeserver.js"></script>
in order access variables in nodeserver.js. error "uncaught referenceerror: require not defined " can solve , have line code : such knows socket.io syntax is. error exists.
what should make html file know socket.io.js.
edit:
the last comment on checked answer solved issue. @houseman.
i'm pretty sure can't throw node.js file html dom , expect work. node.js files run node.js, not client-side html javascript.
this line <script src="http://localhost:8000/socket.io/socket.io.js"></script>
sufficient loading io
object dom, , make accessible through javascript.
remove line:
<script type="text/javascript" src="nodeserver.js"></script>
, , should fine, if server running on port 8000. server code, however, doesn't seem have necessary components run.
edit:
if want pass variables client server, or other way around, use sockets, you're doing.
Comments
Post a Comment