使用 node js 和 UCWA 的聊天应用程序
Chat Application using node js and UCWA
我正在尝试使用节点 js 创建一个聊天应用程序,并且 ucwa.I 想将消息从节点 js 应用程序发送到我正在使用的已注册 lync user.For ucwa.I 可以通过调用 ucwa 来验证、授权和创建应用程序 apis.After 我需要一些帮助来将 IM 发送到已注册的 lync user.Can 有人请帮我一些代码示例吗??
我正在使用 中的代码。
这一步之后如何通过node js和UCWA发送IM?
以下是我的代码:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
var http = require('request-promise');
var lync = {};
lync._authorize = function(){
var self = this;
var orgDomain = self.urls.user.match(/https:\/\/([\w\d\.]+)/i)[0];
console.log('orgDomain==>'+orgDomain);
http.get(self.urls.user).catch(function(err){
if(err.statusCode == 401){
var toParse = err.response.headers['www-authenticate'];
var Oauth = toParse.match(/https:\/\/[\d\w\./_-]+/i)[0];
console.log('Oauth==>'+Oauth);
var loginPost = {
grant_type: 'password',
username: self.username,
password: self.password
};
return http.post(Oauth, {form:loginPost});
}
}).then(function(data){
if(data) {
var parsed = JSON.parse(data);
//setup authorization
http = http.defaults({
headers: {Authorization: parsed.token_type + ' ' + parsed.access_token}
});
return http.get(self.urls.user);
}
}).then(function(data){
//check for split-domain scenario
var parsed = JSON.parse(data);
var domain = parsed._links.self.href.match(/https:\/\/([\w\d\.]+)/i)[0];
console.log('[1] '+orgDomain);
console.log('[2] '+domain);
if(domain!== orgDomain){
//split domain scenario
self.urls.user = self.urls.user.replace(orgDomain, domain);
http = http.defaults({
headers: {Authorization: null}
});
self._authorize();
} else { //create app
var parsed = JSON.parse(data);
self.urls.applications = parsed._links.applications.href;
var registerApp = {
culture : "en-us",
endpointId : "2d9dc28d-4673-4035-825c-feb64be28e4e",
userAgent : "NodeJs client"
};
return http.post(self.urls.applications, {body: registerApp,json:true});
}
}).then(function(app){//start message api
console.log("inside start message api=>"+app);
var parsed = JSON.parse(app);
self.urls.startmessege=parsed._embedded.communication._links.startMessaging.href;
var startMessage =
{
"importance":"Normal",
"sessionContext":"33dc0ef6-0570-4467-bb7e-49fcbea8e944",
"subject":"Task Sample",
"telemetryId":null,
"to":"sip:lenea@contoso.com",
"operationId":"5028e824-2268-4b14-9e59-1abad65ff39"
};
return http.post(self.urls.startmessege, {body: startMessage, json:true});
}).then(function(app){//events api
console.log("inside events api=>"+app);
var parsed = JSON.parse(app);
self.urls.events=parsed._links.events.href;
return http.get(self.urls.events);
}).then(function(app){//events next api
console.log("inside events next api=>"+app);
var parsed = JSON.parse(app);
self.urls.events.next=parsed._links.next.href;
return http.get(self.urls.events.next);
}).then(function(app){//send message api
console.log("inside send message api=>"+app);
var parsed = JSON.parse(app);
self.urls.sendmessage=parsed._embedded.messaging._links.sendMessage.href;
self.urls.sendmessage=self.urls.sendmessage+"?OperationContext=5028e824-2268-4b14-9e59-1abad65ff39"
var message=
{
"data":"Hello World",
};
return http.post(self.urls.sendmessage, {body: message, json:true});
}).then(function(app){
console.log(app);
});
};
lync.setup = function(email, password){
var self = this;
var hostname = email.split('@');
this.username = email;
this.password = password;
//discover urls
return http.get('http://lyncdiscover.'+hostname[1])
.then(function(d) {
var parsed = JSON.parse(d);
self.urls = {
self: parsed._links.self.href,
user: parsed._links.user.href,
xframe: parsed._links.xframe.href
};
console.log('self.urls.user==>'+self.urls.user);
console.log('self.urls.self==>'+self.urls.self);
console.log('self.urls.xframe==>'+self.urls.xframe);
return self._authorize();
});
};
//run app
lync.setup('username@domain.com', 'password');
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
看起来您正在正确处理事件,但您没有解析事件结果以查看自上一个周期以来是否发生了任何事情。我建议查看文档 (GettingStarted-Events) 以确保也能处理特殊情况。
您对 startMessaging 的请求看起来不错,但如果不实际查看来自事件通道的事件,就很难知道您是否已正确连接以及对方是否接受了邀请。您会在事件通道中看到一个 messagingInvitation,它应该指示是否 accepted/declined/other 失败。
您对 sendMessage 的请求不正确,因为您无法发送 JSON 样式的请求,而是 text/plain 或 text/html。消息正文是要发送的(未)格式化文本。如果您更正您可能能够发送消息。
当您修复该问题并想知道如何处理收到的消息时,请查看 message 的文档,因为您将再次需要处理事件通道中的数据。
我正在尝试使用节点 js 创建一个聊天应用程序,并且 ucwa.I 想将消息从节点 js 应用程序发送到我正在使用的已注册 lync user.For ucwa.I 可以通过调用 ucwa 来验证、授权和创建应用程序 apis.After 我需要一些帮助来将 IM 发送到已注册的 lync user.Can 有人请帮我一些代码示例吗??
我正在使用
以下是我的代码:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
var http = require('request-promise');
var lync = {};
lync._authorize = function(){
var self = this;
var orgDomain = self.urls.user.match(/https:\/\/([\w\d\.]+)/i)[0];
console.log('orgDomain==>'+orgDomain);
http.get(self.urls.user).catch(function(err){
if(err.statusCode == 401){
var toParse = err.response.headers['www-authenticate'];
var Oauth = toParse.match(/https:\/\/[\d\w\./_-]+/i)[0];
console.log('Oauth==>'+Oauth);
var loginPost = {
grant_type: 'password',
username: self.username,
password: self.password
};
return http.post(Oauth, {form:loginPost});
}
}).then(function(data){
if(data) {
var parsed = JSON.parse(data);
//setup authorization
http = http.defaults({
headers: {Authorization: parsed.token_type + ' ' + parsed.access_token}
});
return http.get(self.urls.user);
}
}).then(function(data){
//check for split-domain scenario
var parsed = JSON.parse(data);
var domain = parsed._links.self.href.match(/https:\/\/([\w\d\.]+)/i)[0];
console.log('[1] '+orgDomain);
console.log('[2] '+domain);
if(domain!== orgDomain){
//split domain scenario
self.urls.user = self.urls.user.replace(orgDomain, domain);
http = http.defaults({
headers: {Authorization: null}
});
self._authorize();
} else { //create app
var parsed = JSON.parse(data);
self.urls.applications = parsed._links.applications.href;
var registerApp = {
culture : "en-us",
endpointId : "2d9dc28d-4673-4035-825c-feb64be28e4e",
userAgent : "NodeJs client"
};
return http.post(self.urls.applications, {body: registerApp,json:true});
}
}).then(function(app){//start message api
console.log("inside start message api=>"+app);
var parsed = JSON.parse(app);
self.urls.startmessege=parsed._embedded.communication._links.startMessaging.href;
var startMessage =
{
"importance":"Normal",
"sessionContext":"33dc0ef6-0570-4467-bb7e-49fcbea8e944",
"subject":"Task Sample",
"telemetryId":null,
"to":"sip:lenea@contoso.com",
"operationId":"5028e824-2268-4b14-9e59-1abad65ff39"
};
return http.post(self.urls.startmessege, {body: startMessage, json:true});
}).then(function(app){//events api
console.log("inside events api=>"+app);
var parsed = JSON.parse(app);
self.urls.events=parsed._links.events.href;
return http.get(self.urls.events);
}).then(function(app){//events next api
console.log("inside events next api=>"+app);
var parsed = JSON.parse(app);
self.urls.events.next=parsed._links.next.href;
return http.get(self.urls.events.next);
}).then(function(app){//send message api
console.log("inside send message api=>"+app);
var parsed = JSON.parse(app);
self.urls.sendmessage=parsed._embedded.messaging._links.sendMessage.href;
self.urls.sendmessage=self.urls.sendmessage+"?OperationContext=5028e824-2268-4b14-9e59-1abad65ff39"
var message=
{
"data":"Hello World",
};
return http.post(self.urls.sendmessage, {body: message, json:true});
}).then(function(app){
console.log(app);
});
};
lync.setup = function(email, password){
var self = this;
var hostname = email.split('@');
this.username = email;
this.password = password;
//discover urls
return http.get('http://lyncdiscover.'+hostname[1])
.then(function(d) {
var parsed = JSON.parse(d);
self.urls = {
self: parsed._links.self.href,
user: parsed._links.user.href,
xframe: parsed._links.xframe.href
};
console.log('self.urls.user==>'+self.urls.user);
console.log('self.urls.self==>'+self.urls.self);
console.log('self.urls.xframe==>'+self.urls.xframe);
return self._authorize();
});
};
//run app
lync.setup('username@domain.com', 'password');
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
看起来您正在正确处理事件,但您没有解析事件结果以查看自上一个周期以来是否发生了任何事情。我建议查看文档 (GettingStarted-Events) 以确保也能处理特殊情况。
您对 startMessaging 的请求看起来不错,但如果不实际查看来自事件通道的事件,就很难知道您是否已正确连接以及对方是否接受了邀请。您会在事件通道中看到一个 messagingInvitation,它应该指示是否 accepted/declined/other 失败。
您对 sendMessage 的请求不正确,因为您无法发送 JSON 样式的请求,而是 text/plain 或 text/html。消息正文是要发送的(未)格式化文本。如果您更正您可能能够发送消息。
当您修复该问题并想知道如何处理收到的消息时,请查看 message 的文档,因为您将再次需要处理事件通道中的数据。