View previous topic :: View next topic |
Author |
Message |
Baaleos
Joined: 02 Sep 2007 Posts: 830
|
Posted: Mon Oct 22, 2012 23:07 Post subject: WebServices with NWN |
|
|
While not real webServices that you might test with SoapUI etc and other industry type tools etc.
I've started building webServices for my nwn server.
The introduction of the DotNet plugin makes this easier for me.
Its probably been possible for alot of the community to do, but I thought I would post an example of how I am planning to use webservices, in the hopes it is useful to anyone.
Ok - so thing to think about - what do you need the web service to do.
Basically, its a website address that you will browse to, give it some arguments, and then it does some sort of calculation/functionality in the backend, and then spits out the result, and gives it back to you the end user.
The below code shows a very basic one I have made, that when used will allow the user to supply a Username of a Forum user, and then the system will give the user that persons Unique ID in the forum database.
This might be something you need, if you want to integrate forums with your online game?
Eg- Do something in game, have it reflected on your forum for specific users?
Code: |
<?php
$request = $_GET["request"];
if($request == "GetForumMemberID")
{
$MemberName = $_GET["membername"];
//echo $MemberName;
echo GetForumMemberID($MemberName);
exit;
}
function GetForumMemberID($strUsername)
{
$username = "yourForumDBUserName";
$password = "YourForumDBPassword";
$con = mysql_connect("localhost",$username,$password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_name", $con);
$result = mysql_query("SELECT * FROM smf_members where member_name like '%".$strUsername."%' limit 1");
while($row = mysql_fetch_array($result))
{
return $row['id_member'];
}
mysql_close($con);
}
?>
|
How this gets called?
Imagine this php file exists at
www.yoursite.com/forumServices.php
Then the call to this function would be
http://www.yoursite.com/forumServices.php?request=GetForumMemberID&membername=Baaleos
If I am ID 1 in the database, it then prints the number 1 on the browser window.
Not spectacular I know.
How do we use this in nwn?
Well - if you are using the DotNetPlugin - and have followed some of the code snippets I have supplied, you could build an uncompiled class file like this
Code: |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.Windows.Forms;
using System.IO;
namespace NwnxAssembly
{
public static class ForumServices
{
public static string GetForumMemberID(string Username)
{
HttpWebRequest wRequest;
HttpWebResponse response;
Stream resStream;
StreamReader reader;
try
{
wRequest = (HttpWebRequest)WebRequest.Create("http://www.yoursite.com/forumServices.php?request=GetForumMemberID&membername="+ Username);
// execute the request
response = (HttpWebResponse)
wRequest.GetResponse();
// we will read data via the response stream
resStream = response.GetResponseStream();
reader = new StreamReader(resStream);
string sText = reader.ReadToEnd();
//MessageBox.Show(sText);
return sText;
}
catch (Exception e)
{
return "Error:" + e.ToString();
}
finally
{
wRequest = null;
response = null;
resStream = null;
reader = null;
}
}
}
}
|
To call this in Nwn you could then call it via nwnscript like this
Code: |
string ForumID = RunClassMethod("ForumServices", "GetForumMemberID", "Baaleos");
SpeakString(ForumID);
|
More code related to this can be found here
http://www.nwnx.org/phpBB2/viewtopic.php?t=1002&start=15
So voila - thats basically a very basic example of how to join a forum to a nwn server.
Made possible via nwnx, and dotNet plugin, and the uncompiled class system I posted earlier.
This system can be taken to the extremes and be made to integrate many aspects of your forum, or online community with nwn.
Maybe even facebook.
Essentially - to add more functionality, you just need to know the php code, and be able to expand your php file to accomplish the things you need. |
|
Back to top |
|
|
Baaleos
Joined: 02 Sep 2007 Posts: 830
|
Posted: Mon Oct 22, 2012 23:33 Post subject: |
|
|
Code: | <?php
$request = $_GET["request"];
if($request == "GetForumMemberID")
{
$MemberName = $_GET["membername"];
//echo $MemberName;
echo GetForumMemberID($MemberName);
exit;
}
if($request == "GetForumUserEmailByID")
{
$MemberID = $_GET["memberid"];
//echo $MemberName;
echo GetForumUserEmailByID($MemberID);
exit;
}
if($request == "GetForumUserEmailByUserName")
{
$MemberName = $_GET["membername"];
//echo $MemberName;
echo GetForumUserEmailByUserName($MemberName);
exit;
}
function GetForumMemberID($strUsername)
{
$username = "DB_User";
$password = "DB_Pass";
$con = mysql_connect("localhost",$username,$password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("your_db", $con);
$result = mysql_query("SELECT * FROM smf_members where member_name like '%".$strUsername."%' limit 1");
while($row = mysql_fetch_array($result))
{
return $row['id_member'];
}
mysql_close($con);
}
function GetForumUserEmailByID($ID)
{
$username = "DB_User";
$password = "DB_Pass";
$con = mysql_connect("localhost",$username,$password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("your_db", $con);
$result = mysql_query("SELECT email_address FROM smf_members where id_member = '".$ID."' limit 1");
while($row = mysql_fetch_array($result))
{
return $row['email_address'];
}
mysql_close($con);
}
function GetForumUserEmailByUserName($UserName)
{
$username = "DB_User";
$password = "DB_Pass";
$con = mysql_connect("localhost",$username,$password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("your_db", $con);
$result = mysql_query("SELECT email_address FROM smf_members where member_name like '%".$UserName."%' limit 1");
while($row = mysql_fetch_array($result))
{
return $row['email_address'];
}
mysql_close($con);
}
?>
|
This is a slightly more functional php page that adds the ability for you to get your Forums users E-mail address via the WebService call.
Note - If you were to impliment this- you would need to take necessary precautions that you are properly protecting your communities data. |
|
Back to top |
|
|
Baaleos
Joined: 02 Sep 2007 Posts: 830
|
Posted: Tue Oct 23, 2012 1:18 Post subject: Tested in game |
|
|
Tested inGame - the delay is barely noticable - its essentially almost as fast as a mysql query, while still faster than a nwn database/campaign retrieve.
Used it to retrieve my e-mail address via the GetForumUserEmailByUserName method. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|