【问题标题】:Build Group chat using socket io in Android在 Android 中使用 socket io 构建群聊
【发布时间】:2017-11-25 06:40:40
【问题描述】:

我想创建一个基于 Android 和 socket.io 功能的群聊应用程序,就像 Whatsapp 一样。

【问题讨论】:

  • 这对于 Stack Overflow 来说太宽泛了,但看起来您还是收到了一些相当完整的答案,然后没有回复?从那以后你似乎问了几个问题。

标签: android node.js socket.io


【解决方案1】:

我想我不太明白这个问题,但这是我几年前用 socket.io 写的一个非常小的简单聊天室。我希望你能把它当成骨架..

<body><ul id="Messages"></ul>
<form action="">
    <input id="Message" autocomplete="off">
    <input type="button" id="Send" value="Send">
</form>


<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
    $.getScript('socket.io/socket.io.js', function () {
        initSocket();
    });

    var initSocket = function () {
        var socket = io();

        $('#Send').click(function () {
        var userNameTag = $('#UserNameTag');
        var nameTag = userNameTag.html();
        if (nameTag) {
            socket.emit('ioChatMessage', { text: nameTag + ':  ' + $('#Message').val(), style: userNameTag[0].style });
        } else {
            socket.emit('ioChatMessage', { text: $('#Message').val() });
        }

        $('#Message').val('');
        return false;
    });

    socket.on('ioChatMessage', function (msg) {
        $('#Messages').append($('<li>').text(msg.text).css(msg.style));
    });

    window.onbeforeunload = function () {
        socket.disconnect();
    };
};
    </script>
</body>

App.js

var app = require('express')();
var http = require('http').Server(app);
var socket = require('socket.io')(http);

app.get('*', function(req, res){
    res.sendFile(__dirname + '/index.html');
});

socket.on('connection', function (io) {
    io.on('ioChatMessage', function (msg) {
        socket.emit('ioChatMessage', msg);
    });
});

http.listen(process.env.PORT);

【讨论】:

  • 这当然是假设您已经有节点/网络服务器设置
【解决方案2】:

在此页面中,您可以了解如何在浏览器中使用 socket.io 建立聊天组:

https://socket.io/get-started/chat/

如果您希望它成为 Android 应用程序,我的建议是按照您在教程中看到的那样进行操作,然后使用 Apache Cordova 使其成为任何人都可以安装的 Android 应用程序。

您可以从这里下载:

https://cordova.apache.org/

最终的 index.html 应该如下所示:

<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
      #messages { margin-bottom: 40px }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
    <script>
      $(function () {
        var socket = io();
        $('form').submit(function(){
          socket.emit('chat message', $('#m').val());
          $('#m').val('');
          return false;
        });
        socket.on('chat message', function(msg){
          $('#messages').append($('<li>').text(msg));
          window.scrollTo(0, document.body.scrollHeight);
        });
      });
    </script>
  </body>
</html>

还有 index.js:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 3000;

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});

http.listen(port, function(){
  console.log('listening on *:' + port);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-08
    • 2018-08-07
    • 2017-11-18
    • 2013-04-20
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多