{"action":"created","issue":{"url":"https://api.github.com/repos/Easy-MJ/esay-bug-library/issues/21","repository_url":"https://api.github.com/repos/Easy-MJ/esay-bug-library","labels_url":"https://api.github.com/repos/Easy-MJ/esay-bug-library/issues/21/labels{/name}","comments_url":"https://api.github.com/repos/Easy-MJ/esay-bug-library/issues/21/comments","events_url":"https://api.github.com/repos/Easy-MJ/esay-bug-library/issues/21/events","html_url":"https://github.com/Easy-MJ/esay-bug-library/issues/21","id":324319740,"number":21,"title":"#14 socket.io+node即时通讯","user":{"login":"Easy-MJ","id":11886313,"avatar_url":"https://avatars0.githubusercontent.com/u/11886313?v=4","gravatar_id":"","url":"https://api.github.com/users/Easy-MJ","html_url":"https://github.com/Easy-MJ","followers_url":"https://api.github.com/users/Easy-MJ/followers","following_url":"https://api.github.com/users/Easy-MJ/following{/other_user}","gists_url":"https://api.github.com/users/Easy-MJ/gists{/gist_id}","starred_url":"https://api.github.com/users/Easy-MJ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Easy-MJ/subscriptions","organizations_url":"https://api.github.com/users/Easy-MJ/orgs","repos_url":"https://api.github.com/users/Easy-MJ/repos","events_url":"https://api.github.com/users/Easy-MJ/events{/privacy}","received_events_url":"https://api.github.com/users/Easy-MJ/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-05-18T08:38:04Z","updated_at":"2018-05-18T08:40:26Z","closed_at":null,"author_association":"OWNER","body":"- clinet.html\r\n\r\n``` html\r\n<!doctype html>\r\n<html>\r\n  <head>\r\n      <meta charset=\"UTF-8\">\r\n      <title>Socket.IO chat</title>\r\n      <style>\r\n        * { margin: 0; padding: 0; box-sizing: border-box; }\r\n        body { font: 13px Helvetica, Arial; }\r\n        form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; height: 45px;}\r\n        form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }\r\n        form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }\r\n        #messages { list-style-type: none; margin: 0; padding: 0; margin-bottom: 45px;}\r\n        #messages li { padding: 5px 10px; }\r\n        #messages li:nth-child(odd) { background: #eee; }\r\n      </style>\r\n  </head>\r\n  <body>\r\n    <div id=\"register\" onclick=\"register\">\r\n        <input id=\"inp\" autocomplete=\"off\" />\r\n        <button>进入聊天室</button>\r\n    </div>\r\n    <div id=\"chat\" style=\"display: none;\">\r\n        <ul id=\"messages\"></ul>\r\n        <form action=\"\">\r\n          <input id=\"m\" autocomplete=\"off\" />\r\n          <button>Send</button>\r\n        </form>\r\n    </div>\r\n  </body>\r\n  <script src=\"https://cdn.bootcss.com/socket.io/2.1.0/socket.io.js\"></script>\r\n  <script src=\"https://code.jquery.com/jquery-1.11.1.js\"></script>\r\n  <script>\r\n\r\n    $(function () {\r\n        var name = ''\r\n        var socket = io('http://10.16.116.13:8083');\r\n        $('#register button').on('click',function(){\r\n            if($('#inp').val()){\r\n                name = $('#inp').val();\r\n                $('#register').hide();\r\n                $('#chat').show();\r\n                //发送昵称给服务器\r\n                socket.emit('my other event', {name:name});\r\n            }\r\n            else{\r\n                alert('请输入昵称')\r\n            }\r\n        });\r\n\r\n        //用户连接时触发\r\n        socket.on('news', function (data) {\r\n            $('#messages').append($('<li>').text(data + '加入群聊 '));\r\n        });\r\n\r\n        //发送数据给服务器\r\n        $('form').submit(function(){\r\n            socket.emit('chat message', {val:$('#m').val(),name:name});\r\n            $('#m').val('');\r\n            return false;\r\n        });\r\n\r\n        //显示聊天信息\r\n        socket.on('chat', function(msg){\r\n            $('#messages').append($('<li>').text(msg.name+'  '+msg.time));\r\n            $('#messages').append($('<li>').text(msg.msg));\r\n        });\r\n\r\n        //用户离开\r\n        socket.on('leave',function(msg){\r\n            $('#messages').append($('<li>').text(msg + '退出群聊 '));\r\n        })\r\n    });\r\n\r\n  </script>\r\n</html>\r\n```\r\n\r\n- server.js\r\n\r\n``` js\r\n/*\r\n* @Author: mjgao@isscloud.com\r\n* @Date:   2018-05-18 16:13:26\r\n* @Last Modified by:   mjgao@isscloud.com\r\n* @Last Modified time: 2018-05-18 16:13:26\r\n*/\r\nvar fs = require('fs');\r\nvar app = require('http').createServer(handler)\r\nvar io = require('socket.io')(app);\r\nvar format = require('./formatTime.js')\r\n\r\nfunction handler (req, res) {\r\n  fs.readFile(__dirname + '/index.html', function (err, data) {\r\n    if (err) {\r\n      res.writeHead(500);\r\n      return res.end('Error loading index.html');\r\n    }\r\n\r\n    res.writeHead(200);\r\n    res.end(data);\r\n  });\r\n}\r\n\r\napp.listen(8083, function(err, res) {\r\n  if(err)\r\n    console.log('err:'+err);\r\n  else\r\n    console.log('server is listening on port 8083');\r\n});\r\n\r\nio.on('connection', function (socket) {\r\n\t  socket.on('my other event', function (data) {\r\n\t\t    socket.name = data.name;\r\n\t\t    io.emit('news', data.name);\r\n\t  });\r\n\r\n\t  socket.on('chat message', function(msg){\r\n\t    \tio.sockets.emit('chat', {msg:msg.val,name:msg.name,time:format.formatter(new Date(),'hh:mm:ss')});\r\n\t  });\r\n\r\n\t  // 发生错误时触发\r\n\t  socket.on('error', (error) => {\r\n\t    // ...\r\n\t  });\r\n\r\n\t  //断开连接时触发\r\n\t  socket.on('disconnect', function (data) {\r\n\t        io.sockets.emit('leave', socket.name);\r\n\t  });\r\n\r\n});\r\n```\r\n\r\n- formatTime.js\r\n\r\n``` js\r\n/*\r\n* @Author: mjgao@isscloud.com\r\n* @Date:   2018-05-18 16:14:03\r\n* @Last Modified by:   mjgao@isscloud.com\r\n* @Last Modified time: 2018-05-18 16:14:03\r\n*/\r\nexports.formatter = function (date, formatString) {\r\n    if (!(date instanceof Date)) {\r\n        date = new Date(date);\r\n    }\r\n    formatString || (formatString = 'YY/MM/DD hh:mm:ss');\r\n    var dateValue = {\r\n        Y : date.getFullYear().toString().slice(-2),\r\n        YY : date.getFullYear(),\r\n        M : date.getMonth() + 1,\r\n        MM : ('0' + (date.getMonth() + 1)).slice(-2),\r\n        D : date.getDate(),\r\n        DD : ('0' + date.getDate()).slice(-2),\r\n        h : date.getHours(),\r\n        hh : ('0' + date.getHours()).slice(-2),\r\n        m : date.getMinutes(),\r\n        mm : ('0' + date.getMinutes()).slice(-2),\r\n        s : date.getSeconds() + 1,\r\n        ss : ('0' + date.getSeconds()).slice(-2)\r\n    };\r\n    return formatString.replace(/((?:Y|M|D|h|m|s){1,2})/g, function ($0, $1) {\r\n        return dateValue[$1];\r\n    });\r\n};  \r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/Easy-MJ/esay-bug-library/issues/comments/390137943","html_url":"https://github.com/Easy-MJ/esay-bug-library/issues/21#issuecomment-390137943","issue_url":"https://api.github.com/repos/Easy-MJ/esay-bug-library/issues/21","id":390137943,"user":{"login":"Easy-MJ","id":11886313,"avatar_url":"https://avatars0.githubusercontent.com/u/11886313?v=4","gravatar_id":"","url":"https://api.github.com/users/Easy-MJ","html_url":"https://github.com/Easy-MJ","followers_url":"https://api.github.com/users/Easy-MJ/followers","following_url":"https://api.github.com/users/Easy-MJ/following{/other_user}","gists_url":"https://api.github.com/users/Easy-MJ/gists{/gist_id}","starred_url":"https://api.github.com/users/Easy-MJ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Easy-MJ/subscriptions","organizations_url":"https://api.github.com/users/Easy-MJ/orgs","repos_url":"https://api.github.com/users/Easy-MJ/repos","events_url":"https://api.github.com/users/Easy-MJ/events{/privacy}","received_events_url":"https://api.github.com/users/Easy-MJ/received_events","type":"User","site_admin":false},"created_at":"2018-05-18T08:40:26Z","updated_at":"2018-05-18T08:40:26Z","author_association":"OWNER","body":"- package.json\r\n\r\n``` bash\r\n{\r\n  \"name\": \"socket-chat-example\",\r\n  \"version\": \"0.0.1\",\r\n  \"description\": \"my first socket.io app\",\r\n  \"dependencies\": {\r\n    \"socket.io\": \"^2.1.1\"\r\n  }\r\n}\r\n```"}}
{"actor":{"display_login":"Easy-MJ"}}