Pomelo 使用实列
时间: 2023-07-11 11:22:25 浏览: 91
pomelo 2.2.5
假设你已经安装了Node.js和npm,下面是一个简单的Pomelo应用程序的示例:
1. 创建一个新的Pomelo应用程序
```
npm install -g pomelo-generator
pomelo init myapp
cd myapp
npm install
```
2. 启动Pomelo应用程序
```
pomelo start
```
3. 创建一个简单的聊天服务器
在myapp目录中创建一个新目录chat,并在chat目录中创建一个新文件chat.js:
```
module.exports = function(app) {
return new ChatHandler(app);
};
var ChatHandler = function(app) {
this.app = app;
};
ChatHandler.prototype.send = function(msg, session, next) {
var rid = session.get('rid');
var username = session.get('username');
var channelService = this.app.get('channelService');
var param = {
msg: msg.content,
from: username,
target: msg.target
};
channel = channelService.getChannel(rid, false);
channel.pushMessage('onChat', param);
next(null, {
route: msg.route
});
};
```
4. 创建一个简单的聊天客户端
在myapp目录中创建一个新目录web-server,并在web-server目录中创建一个新文件index.html:
```
<html>
<head>
<title>Chat Client</title>
<script src="/pomelo.js"></script>
</head>
<body>
<div id="chat">
<input id="username" type="text" placeholder="username"/>
<input id="submit" type="button" value="Join"/>
<input id="leave" type="button" value="Leave"/>
<br />
<textarea id="content" rows="10" cols="50"></textarea>
<br />
<input id="to" type="text" placeholder="to"/>:
<input id="msg" type="text" placeholder="message"/>
<input id="send" type="button" value="Send"/>
</div>
<script>
var pomelo = window.pomelo;
var username = '';
var rid = 'room';
var channel = null;
var route = 'chat.chatHandler.send';
function init() {
pomelo.init({
host: location.hostname,
port: 3014,
log: true
}, function() {
console.log('init success');
});
}
function login() {
username = document.getElementById('username').value;
pomelo.request('gate.gateHandler.queryEntry', {
uid: username
}, function(data) {
pomelo.disconnect();
pomelo.init({
host: data.host,
port: data.port,
log: true
}, function() {
pomelo.request('connector.entryHandler.enter', {
username: username,
rid: rid
}, function(data) {
console.log(data);
channel = pomelo.subscribe('chat' + rid);
channel.on('onChat', function(data) {
addMessage(data.from, data.msg);
});
});
});
});
}
function addMessage(from, msg) {
var content = document.getElementById('content');
content.value = content.value + from + ":" + msg + "\r\n";
}
function leave() {
pomelo.request('connector.entryHandler.leave', {
username: username,
rid: rid
}, function(data) {
console.log(data);
});
}
function send() {
var target = document.getElementById('to').value;
var msg = document.getElementById('msg').value;
pomelo.request(route, {
rid: rid,
content: msg,
from: username,
target: target
}, function(data) {
console.log(data);
});
}
init();
document.getElementById('submit').addEventListener('click', login);
document.getElementById('leave').addEventListener('click', leave);
document.getElementById('send').addEventListener('click', send);
</script>
</body>
</html>
```
5. 启动Web服务器
在myapp目录中创建一个新目录web-server,并在web-server目录中创建一个新文件app.js:
```
var express = require('express');
var path = require('path');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.listen(3000, function() {
console.log('Web server started at http://localhost:3000');
});
```
6. 启动Pomelo和Web服务器
在myapp目录中运行以下命令:
```
pomelo start
cd web-server
node app.js
```
现在你可以通过浏览器访问http://localhost:3000,输入用户名并点击Join按钮加入聊天室。你还可以使用Leave按钮离开聊天室,使用Send按钮发送消息给其他用户,或者使用To输入框指定目标用户。
阅读全文