• 使用nodejs引用socket.io做聊天室


    Server:

    var app = require('http').createServer(handler)
      , io = require('socket.io').listen(app)
      , fs = require('fs');
    
    app.listen(80);
    console.log('server listen on port 80');
    
    function handler (req, res) {
      fs.readFile(__dirname + '/public/index.html',
      function (err, data) {
        if (err) {
          res.writeHead(500);
          return res.end('Error loading index.html');
        }
    
        res.writeHead(200);
        res.end(data);
      });
    }
    
    var chatroom=io.of('/chat').on('connection',function(socket){
        console.log(id+'connected');
        var id=null;
        socket.on('setid',function(sid){
            id=sid;
            socket.emit('id',id);
            chatroom.emit('msg',id+' join the chatroom');
        });
        socket.on('msg',function(msg){
            chatroom.emit('msg',id+' : '+msg);
        });
    })

    Client:

    <!doctype html>
    <html>
    <head>
        <title>socket.io</title>
        <script type="text/javascript" src="/socket.io/socket.io.js"></script>
        <style type="text/css">
        body{
            font: message-box;
        }
        #output{
            height:200px;
            border:1px solid #888;
            overflow-y: scroll;
        }
        </style>
    </head>
    <body>
        <div id="output"></div>
        <span id="userid"></span>
        <input type="text" id="text" placeholder='input your name'>
        <button id="btn">connect</button>
        
        <script type="text/javascript">
            var socket = null;
            var btn=document.getElementById('btn');
            var text=document.getElementById('text');
            var output=document.getElementById('output');
            btn.onclick=function(e){
                console.log('btn click');
                socket = io.connect('http://localhost/chat');
                socket.on('connect',function(){
                    console.log('connect');
                    socket.emit('setid',text.value);
                    socket.on('id',function(id){
                        document.getElementById('userid').innerText=id+':';
                    });
                    socket.on('msg',function(msg){
                        output.innerText+=(msg+'
    ');
                        output.scrollTop=output.scrollHeight;
                    });
                    btn.innerText='send';
                    text.placeholder='input msg';
                    text.value='';
                    btn.onclick=function(e){
                        socket.emit('msg',text.value);
                        text.value='';
                    }
                });
            };
        </script>
    </body>
    </html>
  • 相关阅读:
    学Java,上这个网站就够了
    父类如果不写无参构造方法,子类会报错
    Java中instanceof的用法
    Spring,SpringMVC,Mybatis等配置文件报错解决(Referenced file contains errors)
    初识Java
    JavaSE课程知识体系总结
    Java中awt和swing的关系和区别
    ServiceMesh实验室——00之实验室搭建
    Microservices==>Service Mesh==>Serverless,走马观花
    领域驱动(DDD)之我见,基于Golang实现
  • 原文地址:https://www.cnblogs.com/jordan2009/p/3648387.html
Copyright © 2020-2023  润新知