|
服务端:
- var io = require('socket.io')(1234);
- io.sockets.on('connection', (client) => {
- client.on('message', function (msg) { //监听到信息处理
- client.send('服务器收到了信息:' + msg);
- });
- client.on("disconnect", function () { //断开处理
- console.log("client has disconnected");
- });
- });
- console.log("listen 1234...");
客户端:
- $(function () {
- var ioiosocket = io.connect('http://localhost:1234/');
- var $ul = $("ul");
- var $input = $("input");
- iosocket.on('connect', function () { //接通处理
- $ul.append($('<li>连上啦</li>'));
- iosocket.on('message', function (message) { //收到信息处理
- $ul.append($('<li></li>').text(message));
- });
- iosocket.on('disconnect', function () { //断开处理
- $ul.append('<li>Disconnected</li>');
- });
- });
- $input.keypress(function (event) {
- if (event.which == 13) { //回车
- event.preventDefault();
- console.log("send : " + $input.val());
- iosocket.send($input.val());
- $input.val('');
- }
- });
- });
Websocket既然能支持跨域方法,那就是说,一个开放给公网的Websocket服务任何人都能访问,这样的话会使数据变得很不安全,所以可以通过对连接域名进行认证即可。
服务器反代
(编辑:PHP编程网 - 湛江站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|